Hello friends, congratulations on completing two days. Let’s dive into the main python course. Today we will learn about 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?

airline="AirIndia" luggage_weight=28 AI_weight_limit=30 EM_weight_limit=35 if(airline=="AirIndia"): if(luggage_weight<=AI_weight_limit): print("Check-in cleared") else: print("Remove some luggage and come back") elif(airline=="Emirates"): if(luggage_weight<=EM_weight_limit): print("Check in cleared") else: print("Remove some luggage and come back") else: 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:-


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.
no_of_landings=356 no_of_takeoffs=245 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
Python | if, else, for, while, def, print, raise, try, except |
---|---|
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


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.
- Brackets have the highest precedence followed by orders.
- Modulo, Division, and Multiplication have the same precedence. Hence if all appears in an expression, it is evaluated from Left to Right.
- 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?
a=356 b=245 c=89 d=a+c-b print(d)
'''This is a program to find the current number of flights in an airport''' landings_count=356 takeoffs_count=245 initial_flights=89 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
Questions | Options |
---|---|
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 |
Answers ---> 1.true 2.false 3.false 4.true 5.true 6.false 7.true 8.true 9.false
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,
- Converting a floating point value to integer would result in loss of decimal point values.
- A larger data type if converted to smaller data type will result in loss of data as the number will be truncated.
Conversion | Python | ||
---|---|---|---|
Conversion to int | int() Example: num=int(“10”) Value of num will be 10 | ||
Conversion to string | str() Example: num=str(10) Value of num will be “10” |
GIPHY App Key not set. Please check settings