6 Session Python Fundamentals Part 3
6 Session Python Fundamentals Part 3
MEMBERSHIP OPERATOR
•in- Whether variable in sequence
• not in- Whether variable not in sequence
Examples
They are the symbols that are used in programming languages to organize
programming-sentence structure and indicate the rhythm and emphasis of
expression, statements and program structure.
Examples:-
‘ “ # \ () [] {}
@ , : . ;
PYTHON FUNDAMENTALS
PUNCTUATORS
Example:- use of semicolon(;)
x=10
y=20
print(x+y);print(x-y);print(x*y) # not recommended
Example :- use of colon(:)
x=10
y=20
if x>=y: # marking of if block
print(“x is greater than y”)
print(“Happy to see that”)
else : # marking of else block
print(“y is greater than x”)
print(“Not so happy”)
PYTHON FUNDAMENTALS
EXPRESSIONS
An expression is anylegal combination of symbols that represents a value.
Examples:-
A+10
(B+C)*40
(3+5)/2
STATEMENT
A statement is a programming instruction that does something. i.e some action
takes place. (Note:- A statement may or may not yield a value)
Examples:-
print(“HELLO”) # action took place
B=10+30 # an expression is evaluated a sttaement is executed
PYTHON FUNDAMENTALS
COMMENTS
Comments are additional readable information to clarify the source code for
the programmers. These are non executable statement(s) which are ignored by
the INTERPRETER. Comments in Python begin with # and generally ends with
the physical line.
print(“Harshdeep Singh”)
print(“XI – SA1”)
print(“”House no. 435, Sector-15-A”)
print(“Faridabad\tHaryana”)
PYTHON FUNDAMENTALS
FUNCTIONS
A function is a code that has a name and can be reused by specifying its name in
the program when it is needed.
(We have built-in functions, module functions and user defined functions)
Totalmarks
. 460
name
. ‘Vidhi Sharma’
PYTHON FUNDAMENTALS
VARIABLES and ASSIGNMENT
Example:-
CREATE A VARIABLE:-
amount=50000.5 #type float
amount
. 50000.5
Example:- x . 120
z
. ‘apple’
x=120
y=40.5
y . 40.5
z=‘apple’
PYTHON FUNDAMENTALS
VARIABLES and ASSIGNMENT
Dynamic Typing:- A variable pointing to a value of a certain type can be made to
point to value/object of different type.
Example:-
A=‘’ADITYA”
B=A
C=B “ADITYA”
A=9.5
B=A
D=‘JASPREET’
A .
B .
C
. 9.5
D
. “JASPREET”
PYTHON FUNDAMENTALS
VARIABLES and ASSIGNMENT
Dynamic Typing:- A variable pointing to a value of a certain type can be made to
point to value/object of different type.
Example:-
A=‘’ADITYA”
B=A
C=B
A=9.5
B=A
D=‘JASPREET’
print( A)
print(B)
print(C)
print(D)
OUTPUT
9.5
9.5
ADITYA
JASPREET
PYTHON FUNDAMENTALS
VARIABLES and ASSIGNMENT
type() – this function is an built-in function which will tell the type of the
object(variable\literal)
EXAMPLE:-
x=10
y=6.789
Amount=10000000000
name =‘’Arvind Gaur”
print(type(x))
print(type(y))
print(type(Amount))
print(type(name))
PYTHON FUNDAMENTALS
VARIABLES and ASSIGNMENT
type() – this function is an built-in function which will tell the type of the
object(variable\literal)
OUTPUT:-
int
float
int
str
PYTHON FUNDAMENTALS
VARIABLES and ASSIGNMENT
Lvalue and Rvalue of a variable
Lvalue:- that comes on the left hand side of an assignment. Object to which you
assign the value.
Rvalue:- that comes on the right hand side of an assignment. They are the literals
and expression that are assigned to lvalue.
z
Example:-
A=10
B=20.5
Example:-
a,b,c=5,10,20
b,c,a=a+1,b+2,c-1
print(a,b,c)
OUTPUT
19 6 12
PYTHON FUNDAMENTALS
VARIABLES and ASSIGNMENT
Multiple assignments
Example:-
Q State ouput of the following :-
1)
p,q =3,5
q,r = p-2,p+2
print(p,”\t”, q, ”\t” , r)
2)
x=10
y,y=x+2, x+5
print(y)
3)
x,x=20,30
y,y=x+10,x+20
print (x, ”\t” , y)
PYTHON FUNDAMENTALS
VARIABLES and ASSIGNMENT
Multiple assignments
Example:-
ANSWER:-
1. 3 1 5
2. 15
3. 30 50
PYTHON FUNDAMENTALS
ACCEPT VALUE FROM THE USER
input()- it is an built in function of Python that allows to get input from the
user interactively (the input always gives string value)
SYNTAX:-
Variable_to_hold_the_value = input(<prompt to be displayed>)
EXAMPLE:-
x= input(“Enter your name”)
y=input(“Enter your Age”)
print(type(x), type(y))
Example:- What is the error in the code? How will you rectify that
error?
Ans:-
Error is in statement 3 where an str value is added to int value
To add we have to first convert age into int value by using int() function.
Q3. Write a program to accept name from the user and print a
welcome message for him/her by name
Q4. Write a program to find area of a triangle.
PYTHON FUNDAMENTALS
OUTPUT through print()
Syntax:-
print(object)
print(object,object,object)
Examples:-
print("MVN-17")
print("MVN ARAVALI HILLS","MVN-88",'MVN-Palwal')
print("MVN SCHOOL\tFARIDABAD\HARYANA\nINDIA")
X=10
y=50.5
z='Information For All'
print("X=",X,"Y=",y)
print("Z=",z)
print("Sum is:",x+y)
print("product is",x*y)
PYTHON FUNDAMENTALS
OUTPUT
NOTE: print()
print() without any value or name or expression will print a
blank line
PYTHON FUNDAMENTALS
• The print function automatically adds the sep character between the
objects/items being printed in a line. The default sep character is space.
Examples:-
>>> print('Apple','Mango','Pear')
Apple Mango Pear
>>> print("My","name","is","Aditya")
My name is Aditya
• The print() with sep argument - we can change the default sep character.
Examples:-
>>> print('Apple','Mango','Pear',sep='#')
Apple#Mango#Pear
>>> print('Apple','Mango','Pear',sep='#$#')
Apple#$#Mango#$#Pear
>>> print('Apple','Mango','Pear',sep='.....')
Apple.....Mango.....Pear
PYTHON FUNDAMENTALS
>>> print('Apple','Mango','Pear',sep='\t')
Apple Mango Pear
>>> print('Apple','Mango','Pear',sep='\t\t\t')
Apple Mango Pear
• print() appends a newline character (‘\n’) at the end of the line unless you
give our own end string with end argument.
Example:-
print("mango","Apple")
print("Orange")
PYTHON FUNDAMENTALS
print() with end argument.
print("Apple","IBM","HCL",end="##")
print("INFOSYS","TATACONSULTANCY",end="$")
print("MICROSOFT","GOOGLE")
PYTHON FUNDAMENTALS
print() with Sep and end argument.
print("IBM","MICROSOFT",sep='-----',end="\t")
print("HCL","GOOGLE",sep='*****',end='$$')
print("INFOSYS","WIPRO",sep='.....',end=' ')
print("APPLE")