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
Reference : https://www.w3schools.com/python/
Today's Learning:
Comments are useful to represent extra information in the python script.
Comment in python:
Single line #
Multiple line """ """ within pair of 3 double quotes
Multiple line ''' ''' within pair of 3 single quote
"""
This is treated as Documentation.
Not a good way to write multiple line comment.
"""
It is also considered as docstring.
Using single line comment is Good way for comment in python.
Output:
Google Meet Session Video (Please rotate mobile screen and watch)
Comments are useful to represent extra information in the python script.
Comment in python:
Single line #
Multiple line """ """ within pair of 3 double quotes
Multiple line ''' ''' within pair of 3 single quote
"""
This is treated as Documentation.
Not a good way to write multiple line comment.
"""
It is also considered as docstring.
Using single line comment is Good way for comment in python.
# single line comment """Multiple line comment""" '''Multiple line comment''' # Comments will not be executed by machine.Output: (No output for above program)
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/4.py
sdbhosale@sdbhosale-Inspiron-N5010:~$ python
Python 2.7.6 (default, Nov 12 2018, 20:00:40)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '''
... Multiple line comments
... Line 1
... Line 2
... Line 3
... Line 4
... '''
'\nMultiple line comments\nLine 1\nLine 2\nLine 3\nLine 4\n'
sdbhosale@sdbhosale-Inspiron-N5010:~$ python
Python 2.7.6 (default, Nov 12 2018, 20:00:40)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '''
... Multiple line comments
... Line 1
... Line 2
... Line 3
... Line 4
... '''
'\nMultiple line comments\nLine 1\nLine 2\nLine 3\nLine 4\n'
Note : Docstrings is executed by python interpreter.
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/16.py
showme function helps me to display result
The start of docstring
The purpose of this function is to display docstring.
Generally docstring gives information of the function.
This is documentation only.
The end of docstring
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/17.py
showme function helps me to display result
This is my class documentation
Class Documentation starts
line 1
line 2
Class Documentation ends
The start of docstring
The purpose of this function is to display docstring.
Generally docstring gives information of the function.
This is documentation only.
The end of docstring
Therefore it is better to use single line comment.
def showme(): '''
The start of docstring
The purpose of this function is to display docstring.
Generally docstring gives information of the function.
This is documentation only.
The end of docstring
'''
print("showme function helps me to display result") showme() #To print docstring#To print Documentation of function
print(showme.__doc__)Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/16.py
showme function helps me to display result
The start of docstring
The purpose of this function is to display docstring.
Generally docstring gives information of the function.
This is documentation only.
The end of docstring
class A(object): '''
This is my class documentation
Class Documentation starts
line 1
line 2
Class Documentation ends
''' def showme(): '''
The start of docstring
The purpose of this function is to display docstring.
Generally docstring gives information of the function.
This is documentation only.
The end of docstring
'''
print("showme function helps me to display result") showme() myobject=A() #To print docstring#To print Documentation of function
#printing class docstring
print(myobject.__doc__) #printing docstring for function of class
print(myobject.showme.__doc__)Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/17.py
showme function helps me to display result
This is my class documentation
Class Documentation starts
line 1
line 2
Class Documentation ends
The start of docstring
The purpose of this function is to display docstring.
Generally docstring gives information of the function.
This is documentation only.
The end of docstring
Therefore it is better to use single line comment.
# Single line comment can be given for multiple lines :
# 1. Please select lines (Shift+Arrow keys) for which you want to pass comment
# 2. then press Ctrl + forwardslash i.e. (Ctrl+/)
#
# Line 1
# Line 2
# Line 3
# Line 4
# Line 5
If you want to display 4 blank lines on the output screen then use following code:
print (4 * "\n") # or:
print ("\n\n\n\n\n")
# for above two statements meaning remain same.
print (4 * "SVERI") print (4 * " SVERI")
Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/6.py
SVERISVERISVERISVERI
SVERI SVERI SVERI SVERI
By default, python's print() function ends with a newline.
print( ) comes with a parameter called 'end.'
The default value of this parameter is '\n'. i.e., the new line character.
You can end a print statement with any character or string using this parameter.
This is available in only in Python 3+
print("Hi",end='\n') print("Hi") # Meaning of first two instructions remains same print ("Welcome to", end = ' ') #print ("TY Mechanical", end = '!',end='\n')
print ("TY Mechanical", end='\n')
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/7.py
Hi
Hi
Welcome to TY Mechanical
print("2+3=",2+3) print("2-3=",2-3) print("2*2=",2*2) #(Integer division or floor division)
print("5//2=",5//2) # To calculate simple quotient
print("8/4=",8/4) print("5/2=",5/2) # To get remainderprint("10%3=",10%3) #multiplication of 2, 3 times
print("2*2*2=",2*2*2) # 2 raise to 2
print("2**2=",2**2) # 2 raise to 3
print("2**3=",2**3)
Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/8.py
2+3= 5
2-3= -1
2*2= 4
5//2= 2
8/4= 2.0
5/2= 2.5
10%3= 1
2*2*2= 8
2**2= 4
2**3= 8
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/9.py
SVERI's COEP
SVERI 'Pandharpur'
SVERI "Pandharpur"
Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/10.py
'SVERI'+'SVERI'+'SVERI'
SVERISVERISVERI
SVERI SVERI SVERI
Specific Program to perform addition of two interger numbers
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/11.py
Value of x= 2
Value of y= 3
Addition of x and y= 5
Generalised Program to perform addition of two integer numbers
#In this case, in the variables string will be stored by default
#string is nothing but collection or group of characters
#input function helps to accept input in the form of string
#We need to convert our string into integer type
#int() # is the standard function in python which will help us to convert string into integer type value
#float() # is the standard function in python which will help us to convert string into float type value
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/1.py
Please enter value of a=2.5
Please enter value of b=2.5
Addition of a and b= 5.0
Addition of x and y= 59
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/13.py
Please enter value of a=5
Please enter value of b=7
Addition of a and b= 12
constant.py file is module here
Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/2.py
Please enter Radius=1.0
Area of circle= 3.141592653589793
Value of pi from constant module= 3.141592653589793
Value of Acceleration due to gravity= 9.81
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/8.py
2+3= 5
2-3= -1
2*2= 4
5//2= 2
8/4= 2.0
5/2= 2.5
10%3= 1
2*2*2= 8
2**2= 4
2**3= 8
#SyntaxError: invalid syntax#print('SVERI's COEP') # Above error is removed by using \ before an apostrophe.
print('SVERI\'s COEP') #SyntaxError: invalid syntax#print("SVERI "Pandharpur"") #use of double and single quotes
print("SVERI 'Pandharpur'") print('SVERI "Pandharpur"')Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/9.py
SVERI's COEP
SVERI 'Pandharpur'
SVERI "Pandharpur"
+ is used to cobine two strings
print("'SVERI'+'SVERI'+'SVERI'") print('SVERI'+'SVERI'+'SVERI') print('SVERI'+' '+'SVERI'+' '+'SVERI')
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/10.py
'SVERI'+'SVERI'+'SVERI'
SVERISVERISVERI
SVERI SVERI SVERI
Specific Program to perform addition of two interger numbers
x=2
y=3
print("Value of x=",x) print("Value of y=",y) print("Addition of x and y=",x+y)Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/11.py
Value of x= 2
Value of y= 3
Addition of x and y= 5
Generalised Program to perform addition of two integer numbers
#In this case, in the variables string will be stored by default
#string is nothing but collection or group of characters
#input function helps to accept input in the form of string
#We need to convert our string into integer type
#int() # is the standard function in python which will help us to convert string into integer type value
#float() # is the standard function in python which will help us to convert string into float type value
#Generalised Program a=float(input("Please enter value of a=")) b=float(input("Please enter value of b=")) c=a+b print("Addition of a and b=",c) x="5"
y="9"
z=x+y print("Addition of x and y=",z)Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/1.py
Please enter value of a=2.5
Please enter value of b=2.5
Addition of a and b= 5.0
Addition of x and y= 59
a=int(input("Please enter value of a=")) b=int(input("Please enter value of b=")) c=a+b print("Addition of a and b=",c)Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/13.py
Please enter value of a=5
Please enter value of b=7
Addition of a and b= 12
a=input("Please enter a=") b=input("Please enter b=") c=int(a)+int(b) print("Addition of a and b=",c)
Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/3.py
Please enter a=5
Please enter b=7
Addition of a and b= 12
#A python program to calculate area of circle r=float(input("Please enter Radius=")) pi=3.14
ac=pi*r*r print("Area of circle=",ac) print("Value of pi in this program=",pi)
Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/15.py
Please enter Radius=1
Area of circle= 3.14
Value of pi in this program= 3.14
#A python program to calculate area of circle import math r=float(input("Please enter Radius=")) ac=math.pi*r*r print("Area of circle=",ac) print("Value of pi from math module=",math.pi)
Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/14.py
Please enter Radius=5
Area of circle= 78.53981633974483
Value of pi from math module= 3.141592653589793
#A python program to calculate area of circle import constant r=float(input("Please enter Radius=")) ac=constant.pi*r*r print("Area of circle=",ac) print("Value of pi from constant module=",constant.pi) print("Value of Acceleration due to gravity=",constant.gravity) #In this case constant will be my module#constant is user defined module
#In this case we accessed value of PI which is defined in constant.py file
constant.py file is module here
pi=3.141592653589793
gravity=9.81
Output:
/home/sdbhosale/PycharmProjects/tyab/venv/bin/python /home/sdbhosale/PycharmProjects/tyab/2.py
Please enter Radius=1.0
Area of circle= 3.141592653589793
Value of pi from constant module= 3.141592653589793
Value of Acceleration due to gravity= 9.81
#sdbhosale
Rohit Adlinge (https://www.linkedin.com/in/rohit-adalinge-524105171) who guided me for docstring and comment. Due to his response in Linkedin, I took a deep dive into docstring and comment in python.
Google Meet Session Video (Please rotate mobile screen and watch)

No comments:
Post a Comment