Table of contents

Variables in python

Let's write a program to print something.

print("Welcome to Raturi Tech Media")

This will output

Welcome to Raturi Tech Media

Now let's see what happened here,

  • print is a python function to print stuff and anything passed inside print()¬†is evaluated and printed on the screen.
  • print("Welcome to Raturi Tech Media") - Any character enclosed within " "¬†gets considered as a string. In our case¬†Welcome to Raturi Tech Media¬†is a string. A string is a type of variable that contains a series of characters.
  • print(1)¬†- 1 is numeric and is considered as int. An int is also another type of variable
  • print(1+2)¬†- 1 and 2 both are numeric or¬†integers and + is an operator¬†that is used to perform the operations on values. Like + there are various other operators and we will cover them later.
  • Like String and Integer, there are various other¬†data types¬†in python.

Now let's define what variables are, Variables are nothing but a memory location to store values assigned to them. In other words, think of a python variable as a container that can store any type of value and of any particular size.

Let create a variable to see how do they work.

name = "Nitin"


Now that we know how to create a variable in python, let's try getting input from a user and storing it in a variable.

age = input("What is your age? ")

What is your age? 24

How to find data type for a variable in python

In the below code snippet you can see that we have assigned each variable with a different value and using the type() method we can check the type of the variable.

a = "Nitin"
b = 24
c = 9.7
d = True

# Output

<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>

Type Casting and Type Conversion in python

The process of converting one data type to another data type is called typecasting.

Let's understand it with an example.

age = input("Enter your age: ")

print("Increment age by 1")

# Output

Enter your age: 24
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

As we can notice that when we tried to add 1 to the age, it gives us an error that says can only concatenate str to str.

Now let's try it again with a different approach

age = input("Enter your age: ")

print("Before type conversion")

print(type(age)) # 'str'

#converting age to int
age = int(age)

print("After type convertion")

print("Increment age by 1")
# Output

Enter your age: 24
Before type conversion
<class 'str'>
After type conversion
<class 'int'>
Increment age by 1

As you can see that it added 1 to the age now.

Arithmetic operations in python

Arithmetic operations are an integral part of every programming language. To do some mathematical operation that involves one or more than one operator, python follows the Operator precedence rule.

Let us understand Operator precedence using an example.

a = 4 + (8 ** 2) - 3 ** 2 % 1 

To find the value of the variable 'a' the following steps have to be followed.

Step 1: The first step is to deal with brackets as it holds the highest precedence among all operators in the given expression. The expression inside these brackets [(8**2)] will get executed first to return 64.

Updated Expression

4 + 64 - 3 ** 2 % 1

Step 2: Moving on, you deal with the exponentiation operator [3**2] as it has the next highest precedence when compared to other operators in the expression.

Updated Expression

4 + 64 - 9 % 1

Step 3: Now, you deal with the remainder operator as it has higher precedence over subtractionandaddition. This means the value 9%1 gets evaluated to return 0.

Updated Expression

4 + 64 - 0

Step 4: In the next step, the addition operator gets executed as it holds higher precedence over subtraction. 

Updated Expression

68 - 0

Step 5: The final step would be to perform subtraction



More information on the operator precedence