Subject : ADVANCED PROGRAMMING CONCEPTS - Python
To solve assignments Please join Google Classroom.
Invitation of Google Classroom has been sent to your official email id.
Please accept the google classroom invitation through official email id.
Or directly by entering Class code of Google Classroom also
you can join Google Class room to solve assignments.
Class code of Google Classroom : 3ojdsjm
With kind regards,
SD Bhosale
- 5 minutes before
To know attendance : Click here
Today's Learning:
We started to write scripts for python language. We wrote first program to display welcome message on output console. We understood role of indentation which is important to represent body/block of statements.
Program 1:
print("Welcome") print('TY Mechanical Engineering Students!!')Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/1.py
Welcome
TY Mechanical Engineering Students!!
Program 2:
print("Welcome") print('TY Mechanical Engineering Students!!')Program 2 gives indentation error:
File "/home/sdbhosale/PycharmProjects/tyab/1.py", line 15
print ("TY Mechanical Engineering students!!")
^
IndentationError: unexpected indent
Indentations are important in python. Indentation represents as body or block of the statements. Reserving some extra space before instruction is called as indentation.
With indentation machine will understand your body is started. In C and C++, we used {} to represent body or block of control (Decision or Looping) statements.
In python, we use indentation.
Program 3:
x=1 if x==1: print ("Welcome") print ("TY Mechanical Engineering students!!") print('SVERI')Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/1.py
Welcome
TY Mechanical Engineering students!!
SVERI
In program 3, three statements are with indentation. It means three statements are part of if block/body. Therefore all statements are executed.
Program 4: (In Decision Control Statement, indentations are used)
x=1 if x!=1: print ("Welcome") print ("TY Mechanical Engineering students!!") print('SVERI')Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/1.py
SVERI
In Program 4, logical condition is changed and last print statement is not part of if body/block as it is not with indentation.
Program 5: (Defining functions in python indentations are used)
def display(): print ("Welcome") print ("TY Mechanical Engineering students!!") print('SVERI') display()Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/1.py
Welcome
TY Mechanical Engineering students!!
SVERI
In Program 5, all three print statements are part of definition of display(). Therefore all are with indentation.
Program 6:
def display(): print ("Welcome") print ("TY Mechanical Engineering students!!") print('SVERI') display()Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/1.py
SVERI
Welcome
TY Mechanical Engineering students!!
In program 6, last print('SVERI') statement is not part of display(). Therefore it has no indentation. As it has no indentation therefore it will be considered as part of main python program. Therefore at the time of execution it will be considered as main statement. So in Program 6, there are two main instructions.
1. printing SVERI
2. callling display function.
Both above statements will be executed as per sequence. As instructions will be executed as per algorithm.
Google Meet Session Video (Please rotate mobile screen and watch)
No comments:
Post a Comment