in , ,

Python operators and variables – 3rd Day of Python Series

Hello friends, congratulations on completing two days. Let’s dive into the main python course. Today we will learn about Python operators and variables.

Python operators and variables

Nishant, Algorithm learning was fun. Can we write a program now?

Sure, But to instruct a computer we need to write a program in a programming language. There are many programming languages in use but we will programming in python.

I’m excited.

We have learned Algorithms based on that can you guess the output?

        print("Check-in cleared")
        print("Remove some luggage and come back")
        print("Check in cleared")
        print("Remove some luggage and come back")
    print("Invalid airline")

I think the output is check-in cleared. But I am confused in this if-else.

Well done! your output is right. Don’t be confused in If- else we will talk about it later. Today, we have to learn about Python operators and variables.

Let’s start with data types.

Python Data types

A program may have data belonging to different data types. Some common data types used in python:-

Python Data types

But, How do we use them?

We have to write the variable name for each data we will declare in our program.

We do not need to write Data-type for data variables in python.

But why?

Because language like python and javascript are Dynamically typed languages.

Dynamic Typing is a technique in some languages where depending on how a value is used, the data type of the variable is dynamically and automatically assigned. Consider the below code in Python,

num=65 #Line 1 
num="A" #Line 2 

In Python, you can find out the data type of a variable by writing type(variable_name)

can we write any name as a variable?

No, there is some reserved word in every programming language. Any name can be given to variables except reserved keywords.

print("Number of landings:",no_of_landings)
print("Number of takeoffs:",no_of_takeoffs)

Print() statement is used to display output in the console.

Match the following:5 min

Reserved keywords in python

Pythonif, else, for, while, def, print, raise, try, except
Python Keywords

If you put a small product in a big box, you may end up wasting space. Similarly if you put a big product in a small box you may end up damaging the product.

Just like product is placed in a box, data occupies memory. Some data need more memory whereas some other data require less memory based on the data type.

Languages like Python and JavaScript automatically create just the right memory needed – neither less nor more.

Whereas in languages like Go, based on the type of data mentioned during variable declaration appropriate memory is allocated.

Variable Scope

We have seen that a variable will have a name, value, type and it will occupy memory. Apart from these, it has two more dimensions – scope and lifetime.

Thus we can say that any variable will have the following six dimensions.

Operators in python

Python Operator
Python Operators

Note: Python, // indicates integer division.
Example: 11//2=5

Precedence of operators

Precedence of an operator can be identified based on the rule – BODMAS. Brackets followed by Orders (Powers, Roots), followed by modulo, Division and Multiplication, followed by Addition and Subtraction.

  1. Brackets have the highest precedence followed by orders.
  2. Modulo, Division, and Multiplication have the same precedence. Hence if all appears in an expression, it is evaluated from Left to Right.
  3. Addition and Subtraction have the same precedence. Hence if both appear in an expression, it is evaluated from Left to Right.

Here are the two codes below. Which one you think is more understandable?

'''This is a program to find the
current number of flights in an airport'''
current_flights = initial_flights + landings_count - takeoffs_count
#Displaying the current number of flights
print("Current number of flights:",current_flights)

The second one is more understandable.

Do you know why?

Check your knowledge

True and True                                                                                                             True/false                                                  
False and True    True/false                                         
1==1 and 2==1                                                                                                              True/false                                                                                                      
1==1 and 2!=1                                                                                                                True/false                                                                                                         
True and 1==1                                                                                                      True/false                                                
“Yes” == “Ok”                                                                                                               True/false                                                                                                             
not(True and False)                                                                                                             True/false                                                                                                          
not(“Landed”==”Landed” and “Flight”==”F101”)                                                                                                               True/false                                                                                                       
1==1 and (not(1==1 or 1==0))                                                                                                                 True/false                                                                                                   
Check your knowledge
Answers --->

What is the output of code snippets below

num1 = 10 num2 = 5 
num2 *= num1 print(num2)

This is same as num2=num2*num1.

output –50

num1 = 50 
num2 = 2 
num3 = 3 
num4 = 8 
result = num1/num4-num3*num2+num4 print(result)

output –8.25

Implicit conversion

We noticed that implicit conversions are dangerous as one may encounter unexpected result.
Similarly, one has to be careful in explicit conversions as well. For example,

  1. Converting a floating point value to integer would result in loss of decimal point values.
  2. A larger data type if converted to smaller data type will result in loss of data as the number will be truncated.
Conversion to intint()

Value of num will be 10

Conversion to stringstr()

Value of num will be “10”
Implicit conversion

What do you think?

Written by Nishant Tyagi


Leave a Reply

Your email address will not be published. Required fields are marked *



Introduction to Algorithms – Day 2

Python Control Structures – 4th Day of Python Series