Intermediate Python (Part 1)

Pasan Devin Jayawardene
Analytics Vidhya
Published in
5 min readJul 26, 2021

--

Data Types, Variables, & Input

C was the first programming language I learned. With C, I was able to cover many fundamental programming concepts such as variables, data structures, functions, file handling etc. I remember when I first started learning Python as someone who has basic programming knowledge, it was a really tedious experience for me. Most of the tutorials I found online was covering fundamental programming knowledge which I already knew. Even though I managed to learn python after going through many necessary and unnecessary Youtube lessons, I still believe that it is not the best Python learning experience for someone with basic programming knowledge. So I thought of writing python tutorial series for intermediate level programmers.

If you have some knowledge of programming fundamentals, have done programming in a language other than Python, and want to learn python programming, then this article series is for you. Be warned that this is not for beginners, but only for intermediate level programmers. Therefore I will mostly be discussing three things. They are,

  1. Basic python syntaxes
  2. The similarities between python and other languages (like C)
  3. The differences between python and other languages

Hello Python

First of all, let’s learn the syntax for printing something to the console with python. following is the code for printing “Hello World” to the console.

Printing something with python

You just have to include the string inside double or single quotations as arguments for the print() function.

Python Comments

You can add a comment to your python code by adding a hash symbol. Following is an example.

Add a comment to python code.

How does python code work?

Anyone interested in learning Python should be aware that python code is not intended for compiling and running. Rather it is interpreted by an interpreter. This means that your code will work even if it is having errors, to the point it finds one. And it will still stop running when it meets such an error (Possibly syntax or runtime error). Following is an example of that.

Errors in python. Note that the code has been executed to the point it has found an error.

Data types in Python

Just like any other programming language, Python also has various data types. In fact, Python has five standard data types. They are,

  1. Numbers
  2. String
  3. List
  4. Tuple
  5. Dictionary

Numbers can be float or integer. Strings are collections of characters or a character itself. Don’t worry about the last three data types for now. I will discuss them in a future article in this series.

Variables

As you probably have learned in programming basics, variables are defined as placeholders for storing data. It’s the same in Python.

But there are some differences as well. One of them is that you don’t need to specify the variable type when you declare a python variable. Also, you can assign a different type of value to a Python variable in the middle of a program (However, this is not recommended). Following is an example code for assigning values to python variables.

Variables in Python

Naming Conventions

Python has some naming conventions when it comes to naming identifiers. You should adhere to these rules when naming a variable, function, class, module, or other object with an identifier. An identifier should always start with a letter (A-Z / a-z) or an underscore (_) followed by zero or more letters, underscores and digits (0-9). Python does not allow any other character such as %,@,( within identifiers. Python is case sensitive. Therefore the words “Hello” and “hello” are considered as two different words in Python.

The following figure shows the Python keywords. These are reserved words that cannot be used as constants, variables, or identifier names. Python’s keywords are all in lowercase letters.

Python Keywords

Numbers in Python

Numbers in python are much more convenient compared to other Programming Languages. Python programmers don’t need to worry about data types when doing calculations with numbers. For example, if you add 12.5 (float) and 15(integer) you get 27.5 which is the desired answer as the output. Following is the code for that example:

Numbers in Python

Python Strings

In Python, a string is defined as a contiguous set of characters represented in quotation marks. Python supports either single or double quotes for strings.

Indexes in Strings

Following are some of the basic things you can do with python Strings.

Strings in Python

Taking Input

Python input() function returns the user keyboard input as a string. You can specify a message in quotations between parentheses to appear in the console when taking the input. Also, you can use the eval() function to convert it into the desired data type. The following code snippets demonstrate a few use-cases of input() and eval() functions.

Taking an input (Note that I have given 10 as the input)
Taking input with a message (The message between parenthesis appears in the console)
Getting inputs and concatenating

You may now have a problem with the output of the above code. You may be thinking that the output should be 30 right? Then why has the program output 1020? Well, here’s the reason for that,

when we give input to a Python program, it will be automatically taken as a string. In this case, both 10 and 20 were taken as strings, not numbers. Consequently, when we add them the strings “10” and “20” are concatenated, resulting in “1020” as the output. The following code brings the solution for this by using help from the eval() function. since the eval() function converts the string into numbers, we get 30 as the output as desired.

Getting the summation of two input values

I guess this is enough for the first article. For your convenience, I’ve included a link to a GitHub repo containing all of the codes that I discussed.

In the next article of this series, I will explain Python lists, tuples, and dictionaries. Until then, Goodbye and Stay Safe!

-Pasan Devin Jayawardene-

--

--

Pasan Devin Jayawardene
Analytics Vidhya

Intern Software Engineer at WSO2 / Software Engineering Undergraduate at University of Kelaniya, Sri Lanka