Python programming zoya
Python programming zoya
ROLL NO :- 5029
#comments:-
It is a readable explanation or annotion in python code and omitted by
compilier. It is denoted by ‘#’
# type of datatype :-
TYPES DENOTED EXAMPLES
STRING Str ‘zoya’
BOOLEAN Bool True
INTEGER Int 9
FLOAT Float 0.9
NONE none None
For example :- a=10
b=2.3
c="hitesh"
print(type(a))
print(type(b))
print(type(c))
#TYPECASTING:-
It is used to find the type of datatype of given variable.there are two
type of typecastingi.e= intersive and extersive
For example :- c=str(input("enter the value for c "))
b=str(input("enter the value for b "))
a=c+b
print(a)
#OPERATORS :-
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Arithmetic operators :-
operators name Example
+ addition A+b
- Subtraction A-b
* Multiplication A*b
/ Division a/b
% Modules A%b
** Exponent A**b
// Floor division a//b
For example:-
emp_id_1=101
emp_id_2=102
emp_id_3=103
p=emp_id_1+emp_id_2+emp_id_3
print("The values are " ,emp_id_1, emp_id_2, emp_id_3)
for example:-
a=8
b=5
c=a-b
s=a*b
d=a/b
print(c)
print(s)
print(d)
print("The ans for your solution is ",c , s ,d)
Assignment operators :-
operator Example
= A=b
+= a+=b
-= a-=b
*= a*=b
/= a/=b
//= a//=b
%= a%=b
Comparison operators:-
operator Example
== A==b
>= a>=b
<= A<=b
!= A!=b
> a>b
< A<b
For example:-
x=2
y=3
print(x==y)
print(x>y)
print(x<y)
print(x<=y)
print(x>=y)
print(x!=y)
Logical operators:-
and Returns true if both X=4
condition is true (x>3) and (x<5)
Or Returns true if one of X=4
them is true (x>3) or (x<5)
Not Reverse result not(x>y or x<y)
not(x>y and x<y)
For example;-
x=15
y=32
print(x>y and x<y)
print(x>y or x<y)
print (not(x>y and x<y))
print (not(x>y or x<y))
#SWAPPING
a=3
b=2
print("Before swapping",a , b)
t=a
a=b
b=t
print("after swapping",a , b)
# MEMBERSHIP OPERATORS:-
fruits=['apple','orange']
print('orange' in fruits)
number=[1,2,34,65,57]
print(98 in number)
# not in
number=[1,2,34,65,57]
print(98 not in number)
fruits=['apple','orange']
print('orange' not in fruits)
#FOR LOOP:-
It is used to iterating over a list. Iterating:- excessing eacg and every
element from a list.
Syntax:-
For variablename in list:
Statement
For example;-
l1=[1,2,3,4,5,6]
for i in l1:
print(i)
l2=['apple','orange','watermelon','melon','kiwi']
for i in l2:
print('i like',i)
#list method
l1.append(67)
print(l1)
l1.extend([12,67,89,0])
print(l1)
print(l1.index(6))
l1.insert(3,67)
print(l1)
l3=[1,5,7,9]
print(l3.count(9))
print(l3.pop())
l3.remove(7)
print(l3)
l1.reverse()
print(l1)
l2.sort()
print(l2)
# LIST METHOD:-
Append() Adds a element at end
Clear() removes all element from list
Extend() Used to add more than one element ij
Index() Index number is written of specific
Insert() Add san element specify index
Count() Return occurrence of specific element
Pop() Remove by default last element
For example:-
l1=['zoya','shafiq','khan','cs','fybsc']
l1.extend(['r programming' ,'dbms'])
print(l1)
l1.append('python')
print(l1)
l1.index('dbms')
print(l1)
l1.insert(5,'vedic maths')
print(l1)
print(l1.pop())
print(l1.remove('fybsc'))
l1.sort()
print(l1)
l1.reverse()
#DICTIONARY:-
a dictionary is a built-in data type that stores data in key-value pairs
keys of dictionary:-
Unordered − The elements in a dictionary do not have a specific
order.
Mutable − You can change, add, or remove items after the dictionary
has been created.
Indexed − Although dictionaries do not have numeric indexes, they
use keys as indexes to access the associated values.
Unique Keys − Each key in a dictionary must be unique.
Heterogeneous − Keys and values in a dictionary can be of any data
type
#methods of dictionary:-
Get() Return value of specified key
For example:-
#change key
subject ={'paper1' :'dsa'}
print (subject)
subject['paper1'] ='maths'
print(subject)
# dictionary with different datatype
dictionary={'brand': 'ford','electric': False ,'year' :1963,'colours' :
['red','orange','green']}
print( dictionary)
thisd = dict(name ='zoya' , age= 17, country = 'india' )
print(thisd)
#accessing
x= thisd['age']
print(x)
print(thisd['name'])
print(dictionary['brand'])
#STRING:-
It encloses between single and double quotation and it is something
like collection of items.
#method of string:-
str4='hello fycs'
print(str4.isspace())
print(str4.replace('fycs ',' rjcollege'))
print(str4.upper())
print(str4.lower())
print(str4.capitalize())
print(str4.count('fyCs'))
print(str4.strip('!'))
print(str4.swapcase())
print(str4.isalnum())
print(str4.title())
print(str4.istitle())
print(str4.find('f'))
print(str4.index('h'))
print(str4.endswith('!'))
print(str4.isalpha())
print(str4.startswith('hi'))
print(str4.split())
print(str4.center(14))
print(str4.isprintable())
print(str4.isupper())
print(str4.islower())
# CONDITIONAL STATEMENT:-
Conditional statement is used to test in python it either evaluates true
or false
Nested if statement:-
test:-
For example:-
#write hello five time
count=0
while count<5:
count=count+1
print('hello')
print('goodbye')
#increment
count=1
while count<5:
count=count+1
print(count)
# decrement
count=6
while count<1:
count=count-1
print(count)
# write a program a sum of natural number
num=int(input('enter your num'))
sum=0
while(num>0):
sum=sum+num
num-=1
print('the sum of natural number',sum)
BREAK STATEMENT:-
break statement is used in while loop to stop execution of loop even
while loop condition is true.
For example:-
i=1
while(i<6):
print(i)
if i==3:
break
i+=1
#CONTINUE IN WHILE LOOP
Continue skip value from output with satisfy condition.
For example:-
i=0
while i<6:
i+=1
if i==3:
continue
print(i)
#PASS STATEMENT:-
pass statement is use as a place holder for future code .we can use
pass statement with loop that we want to use in future
for example:-
n=11
if n>10:
pass
#LIST COMPREHENSION:-
List comprehension is a way of creating a new list based on values of
existing list. It create new list after filtering avalue from old list
depending on specified conditions.
Syntax:-
New list=[expression for item in variable name]
Iterable is nothing but a list
If condition is true.
The expression is the current item in the iteration, but also the
outcome which you can manipulate before it ends up like a list in a
new item.
For example:-
newlis5=['hello' for x in fruits]
print(newlis5)
For example:-
fruits=['apple','mango','watermelon','kiwi','pear']
newlist= [x for x in fruits if 'a' in x ]
print(newlist)
number=[1,2,3,4,5,6,7,8,9,10]
newlist=[x for x in number if x%2==0]
print(newlist)
newlist1=[x for x in number if x%2==1]
print(newlist1)
newlist3=[ x for x in range(10) if x<5]
print(newlist3)
newlist4=[x.upper() for x in fruits]
print(newlist4)
Expression can also contain condition not like filter but as a way to
manipulate the outcome.
For ex:-
newlis6=[x if x!= 'kiwi' else'melon' for x in fruits]
#here x! is used to compare the value of x with given expression
print(newlis6)
condition used in new list
new=[x if x%2==0 else 'odd' for x in number]
print(new)
FUNCTION
function is a block of code written by programmer to perform
specified function built-in function is already defined user just using
it. . function code is reusable in nature instead of writing same code is
again and again we can call same function by passing different input.
Function is declared with def key word. Parameter is also called as
argument.
Syntax of function declaration:-
Def function_name(parameters):
#function body
Function_name()#function call
function execute by function call
for example:-
def fun():
print('welcome')
fun()
def add(a,b):
print(a+b)
add(25,89)
def add(a,name):
print(a)
print('name is', name)
add(67,'jay')
ex4)
def evenOdd(x):
if(x%2==0):
print('even')
else:
print('odd')
evenOdd(2)
hw:-
def exponent(x=int(input('enter your value'))):
print(x**x)
exponent()