Python Tutorial

Soumyabrata Roy
3 min readJun 6, 2021

It is a simple Python tutorial for anyone who wants to learn Python.

Photo by Hitesh Choudhary on Unsplash

Earlier I took the Python lesson from Jose Marcial Portilla from Udemy and if you like you can check out his course here: Python Bootcamps: Learn Python Programming and Code Training | Udemy . Highly Recommended.

Let’s Get Started

I am assuming you have installed Python in your machine. If not, you can install Anaconda distribution (which includes everything) or manually install Python in your machine. Below links could help you out.

Anaconda | Individual Edition

Python 3 Installation & Setup Guide — Real Python

To run your first code, either you can use some IDES like Jupyter Notebook, VS Code or use your terminal and run the python file.

To print anything, we can use print function in Python.

print("Python is good")#output
Python is good

Variables:

In any programming language, variable is something like container or space to store something. Here in Python, it is a simple value. It could be string or some decimal or float number or some other data types. Let’s quickly store your name and print it.

myFirstName = "Soumyabrata"
print(myFirstName)

To declare the variable name, you should use camelCasing (first letter in a word in capital) as I do it here for variable declaration or underscores to connect words my_first_name. but you can not use numbers or some special characters to declare the variables like $myFirstName or 12myFirst etc.

In Python, there are 6 data types. Some of them are Numbers, String, List etc. But all of them are dynamic. Like other low level language, you don’t have to mention them in the variable declaration.

Each data type is different, you could find the type of data you have. You can simply use, type(variable) function to find the data type. Suppose I would like to find the data type of myFirstName, I will write

print(type(myFirstName))#output
<class 'str'>

By the way, to commented out something, you could use # in Python.

Operators

Like other programming language, Python has some build it operators. Like multiplication(*), summation (+), subtraction (-) etc. Here I have given some of the operation in the variable and it’s meanings commented here

variable = 2 + 3variable = variable - 1variable *= 5 #variable = variable * 5variable /= 2 #variable = variable / 2variable += 5 #variable = variable + 5variable +=1 #variable = variable +1
variable -=1 #variable = variable -1
print(variable)#output
15

String and f-strings

In Python, you can handle string in many ways. The most efficient way would be f-strings method. Through the below examples, you will understand it better. I will give the same example using the traditional approach (.format and others) and f-strings approach.

name = "Soumyabrata" 
age = 30
favorite_food = "Chow"

# traditional-l:
print("My name is", name, ", my age is", age, ", and my favorite food is", favorite_food)
# traditional-2:
print("My name is "+ name +", my age is "+ str(age)+ ", and my favorite food is "+ favorite_food)
#using .format:
print("My name is {}, my age is {}, and my favorite food is {}".format(name, age, favorite_food))
#using f-string:
print(f"My name is {name}, my age is {age}, and my favorite food is {favorite_food}")
#output
My name is Soumyabrata , my age is 30 , and my favorite food is Chow

What do you think, which one is the best? using f-string you can create the text very easily and see what is happening inside the code.

You can do the same using .format or other methods but as per my understanding, it is not very user friendly as the f-string. What do you think?

This is what I have learned as of now. I will share my new findings on Python next week. I hope you enjoyed Python so far.

--

--

Soumyabrata Roy

Data Scientist Cognizant | Answering what, why, and how of different business scenarios through machine learning and deep learning.