[go: up one dir, main page]

0% found this document useful (0 votes)
5 views33 pages

6 Session Python Fundamentals Part 3

The document covers fundamental concepts of Python programming, including membership operators, punctuators, expressions, statements, comments, functions, blocks, variables, and assignments. It provides examples for each concept, illustrating how to use them effectively in code. Additionally, it discusses input handling and output formatting using the print function.

Uploaded by

lupinstark7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views33 pages

6 Session Python Fundamentals Part 3

The document covers fundamental concepts of Python programming, including membership operators, punctuators, expressions, statements, comments, functions, blocks, variables, and assignments. It provides examples for each concept, illustrating how to use them effectively in code. Additionally, it discusses input handling and output formatting using the print function.

Uploaded by

lupinstark7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

PYTHON FUNDAMENTALS

MEMBERSHIP OPERATOR
•in- Whether variable in sequence
• not in- Whether variable not in sequence
Examples

>>> x='MVN SCHOOL FARIDABAD'


>>> 'S' in x
True
>>> 'T' in x
False
>>> 'm' in x
False
'S' not in x
False
>>> 'T' not in x
True
>>>
PYTHON FUNDAMENTALS
PUNCTUATORS

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.

SINGLE LINE COMMENT:- (Starts with #)


Example:-

#MY FIRST PYTHON CODE TO TRY VARIABLES OF DIFFERENT TYPE


#the above statement is a full line comment
x=10 # x is of type integer (this is inline comment)
y=‘apple’ # y is a type string
z=4.5 # z is a type float
print(x*z)
print(y*3)
PYTHON FUNDAMENTALS
COMMENTS
MULTI LINE COMMENT:- (put in tripple quotes /apostrophes (‘’’ ‘’’ /””” “”” )
also called as docstring)
Example:-
‘’’
This is a second program program to print my name
Class , Section,
Address and city
‘’’

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)

Example:- (in-built functions)


X=‘ I study in MVN ARAVALI HILLS’
print(x)
print(len(x))
Y=‘’’ MVN ARAVALI SEC-43
FARIDABABAD
HARYANA’’’’
print(len(Y)
PYTHON FUNDAMENTALS
BLOCKS and INDENTATION
A group of statements which are part of another statement or a function are
called block/ code-block/suite

Example:- (block of if statement marked by :(colon))

X=int(input(“Enter your age”))


if X>=18: # if block has two statements)
print(“You are eligible to vote”)
print(“ You are an adult so be execute your right to vote”)
else: # else block gas three statements
Y=18-X
print(“ Sorry not eligible to vote”)
print(“Wait for”, Y ,”years to vote”)
print(“End of program”) # not part of else
PYTHON FUNDAMENTALS
VARIABLES and ASSIGNMENT
A variable in PYTHON is a named location that refers to a value and whose value
can be used and processed during the program run.
A VARIABLE in Python is created when first value is assigned to it.
Variable is a label referring to value
Example:-
CREATE A VARIABLE:-
Totalmarks=460 #type int
name=‘Vidhi Sharma’ #type string

Totalmarks
. 460

name
. ‘Vidhi Sharma’
PYTHON FUNDAMENTALS
VARIABLES and ASSIGNMENT
Example:-
CREATE A VARIABLE:-
amount=50000.5 #type float

amount
. 50000.5

Note: In PYTHON Variable is not a storage container. It’s a reference in Python


PYTHON FUNDAMENTALS
VARIABLES and ASSIGNMENT
In Python assigning a value to a variable means , variable label is referring to that
value.

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

A is the lvalue and 10 is the rvalue


B is the lvalue and 20.5 is the rvalue)
PYTHON FUNDAMENTALS
VARIABLES and ASSIGNMENT
Multiple assignments

1. Assigning same value to multiple variables.


Example:-
a=b=c=10
x=y=z=4.5
2. Assigning multiple values to multiple variables
Example:-
a,b,c=10,20,4.5
x,y=100,200
P,R,T=10000,6.5,10
A,B,C=‘Delhi’,2000,4.5
x,y=y,x
print(x,y) # swapped so will print 200 100
PYTHON FUNDAMENTALS
VARIABLES and ASSIGNMENT
Multiple assignments
Assigning values through multiple assignments , Python first evaluate the RHS
expression(s) and then assign to LHS

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))

NOTE:- both x and y will be of type str only


PYTHON FUNDAMENTALS
Input()

Example:- What is the error in the code? How will you rectify that
error?

name=input(“Enter your name”)


age=input(“Enter your age”)
A=age+10
print(name,”Your age after 10 years will be”,A)
PYTHON FUNDAMENTALS
Input() with int() function

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.

name=input(“Enter your name”) # type of name is str


age=input(“Enter your age”) # type of age is str
A=int(age)+10 # converting age to int()
print(name,”Your age after 10 years will be”,A)
PYTHON FUNDAMENTALS
input() with float() function
float() – built-in function of Python to convert object to float() type
#simple interest program
P= int(input(“Enter principal amount”))
R=float(input(“Enter rate of interest”))
T= int(input(Ënter time period”))
S_I=(P*R*T)/100
print(“Simple Interest is:”, S_I)
print(“Program Over”)
PYTHON FUNDAMENTALS
input()
EXAMPLES:-
#progam to find area and perimeter of rectangle

length = float(input("Enter length"))


breadth= float(input("enter breadth"))
A=length*breadth
P=2*(length+breadth)
print("Area is : ",A)
print("Perimeter is : ",P)
PYTHON FUNDAMENTALS
Home work assignment

Q1. Write a program to find area and perimeter of a square.


Q2. Write a program to find area and circumference of a circle.
(take value of pi as 3.141)

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")

You might also like