Data types:-
- It defines the type of data
- Numbers,List,Tuple,Set,String,Dict.
Number
- It is classified into int(1,30..),float(5.6) and complex(1+2j)
List
- Collection of diff kinds of data
- Each value can be accessed by its index position. List supports both +ve and -ve
index
- Default index value startswith 0
- Supports duplicate values
- It is represented by []
- Mutable(add,update and delete)
- Duplicate values share same index
l = [11,22,33,44,55,66]
0 1 2 3 4 5 => +ve index
-6 -5 -4 -3 -2 -1 => -ve index
'''
l = [11,22,33]
0 1 2
#l[0] = 25 update is possible
#del l[0] delete is possible
#l.append(44) #add new value at the end of the list
print(l)
'''
- Empty list is also possible
- []
- dir(list) - list out all the methods in the list data type
- Most widely used data type in python
- We can perform slicing operations inside list
l.append(44)
print(l)
l.clear()
print(l)
l2 = [10,20]
l = l2.copy()
print(l)
l = [11,22,33,11]
print(l.count(11))
print(l.count(22))
print(l.count(45))
l = [11,22,33,11]
l2 = [44,55,66,77,10]
l2.sort()
print(l2)
l2.reverse()
print(l2)
#pop - last in first order
#l2.pop()
#print(l2)
#l.extend(l2)
#print(l)
#print(l2.index(66))
#l2.insert(2,100)
#l2.insert(1,150)
#l2.remove(44)
print(l2)
l = [11,22,33,44,55]
0 1 2 3 4
Find the index for the below list
l = [10,20,30,40,10,50,60,70,80,20,30,90]
0 1 2 3 0 5 6 7 8 1 2 11
Slicing:-
- Breaking the elements
st: start to till end
st:end - start to end-1
:end - 0 to end-1
: - all
l = [10,20,30,40,50,60,70,80]
0 1 2 3 4 5 6 7
-8 -7 -6 -5 -4 -3 -2 -1
l = [10,20,30,40,50,60,70,80]
print(l[::-1]) #value in reverse order
#reverse order
print(l[::-2]) #skip one value
print(l[::-3]) #skip two values in reverse
print(l[::1]) #same order
print(l[::2]) #skip one value in same order
l = [1,2,3,4,5,6,7,8,9,10]
print(len(l))
print(max(l))
print(min(l))
print(sum(l))
#print the even numbers in the list
#how to use loop?
'''for i in l:
if(i%2==0):
print(i)'''
#print the numbers above 5
'''for i in l:
if(i>5):
print(i)'''
#How to print the sum of last three values
ls = l[-3:]
print(sum(ls))
'''
Task
1. Print the maximum value from first three elements in the list
2. Print the total count of odd numbers in the list
3. Print the values between 3 to 8
'''
Tuple
- Collection of diff kinds of data
- We can access elements using index
- Supports duplicate values
- Immutable -> not possible to add,update or delete
- ()
Example
t = (1,2,3,5+6j,"python",2,3)
print(t[0])
#print(type(t))
#t.append(4) add is not possible
#t[0] = 11 update is not possible
#del t[0] not possible to delete
print(t)
Set
- Does not support duplicate values
- No index
- Union,intersection,difference,disjoint,superset and subset
- It is rep by {}
- Unorder seq of data
- Immutable
- Rarely used dt in python
Example:-
#s = {1,2,3,4,5,6,7,8,9,10,11}
s1 = {20,30}
s1.add(40)
s1.add(50)
print(s1) #We can add values but not able to update/del
#print(s.isdisjoint(s1)) #no common element
#print(s.issuperset(s1))
#print(s1.issubset(s))
#print(s.union(s1))
#print(s.intersection(s1))
#print(s-s1)
#print(s1-s)
Task:-
l = ['apple','mango','pomegranate','apricot']
Print the maximum length of the element in the list
Print the elements startswith vowel
9-10-25
String
- collection of characters
- Each char has its index
- It supports both +ve and -ve index
- String is immutable
- Once string is defined you can't do any modifications
s = input("Enter the string")
#s.append('s') add is not possible
#s[0]='x' #update is not possible
#del s[0] delte is not possible
print(s)
p y t h o n
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1
String Methods:-
1. upper
2. lower
3. len
4. count
5. index,rindex
6. join - join the string
7. split
8. partition
9. center
10. isupper
11. islower
12. isdigit
13. isalpha
14. isalnum
15. capitalize
16. replace
17. title
18. istitle
19. ord
Methods:-
#s = input("Enter the string")
#print(s.upper())
#print(s.lower())
#print(len(s))
#print(s.count('a'))
#count all the char's
#for i in s:
#print(s.count(i))
'''for i in s:
print(s.rindex(i))'''
#abacbd
#012345 - position
#242345 - rindex
#aabbccddefga
#0 1234567891011
#1111335577891011
'''s = "xyz"
s1 = "cde"
s2 = s.join(s1) #cxyzdxyze
print(s2)'''
#s = input("Enter the string")
#print(s.split('a',4)) remove the given char
#print(s.partition('h')) group the char
#print(s.center(11,'$'))
#print(s.isupper())
#print(s.islower())
#print(s.isdigit())
#Get a string from user and count the num of
#up,low and digits
'''u=0
l=0
d=0
for i in s:
if(i.isupper()):
u=u+1
if(i.islower()):
l=l+1
if(i.isdigit()):
d=d+1
print("upper count",u)
print("lower count",l)
print("digit count",d)'''
#s = "123"
#print(s.isalnum())
#s = "python is easy lang"
#print(s.capitalize())
#print(s.replace('o','x'))
#print(s.title())
#print(s.istitle())
#print(ord('A')) #65
#print(ord('a')) #97
Dictionary:-
- It is mutable(add,update and delete).
- It is a combination of key and value pair of data.
- Key is always unique. It is index - []
- Value may be repeated
- Key can be any data type and value can be any data type
- It is denoted {}
- Empty dict is also possible
d = {}
d['a']=1
d['b']=2
d['c']=3
d['d']=4
print(d)
d['b']=20
print(d)
del d['b']
print(d)
Task:-
1. Get a string from user and if it is endswith 's' and length is above six then
replace the last three char's by ing
Example: python = python
abcdefgs = abcdeing
2. l = ["abc","sms","cat","one","tat"]
print the palindrome string in the list
#Access dictionary elements using for loop
d = {'a':11,'b':12,'c':13,'d':14,'e':15}
for i,j in d.items():
print(i)
print(j)
#Methods
#Access dictionary elements using for loop
d = {'a':11,'b':12,'c':13,'d':14,'e':15}
d1 = {'f':16}
#d.update(d1)
#d.clear()
#print(d.keys())
#print(d.values())
#print(d)
#print(d.items())
#d2 = d.copy()
#print(d2)
'''for i,j in d.items():
print(i)
print(j)
#i=>key and j=>value'''
Function:-
- It is a block of code
- It is used to perform a particular task
1. User defined
- It is created by the user. We can identify using def keyword. Function contains
two sections. Function definition and function call.
Function definition:
def fun_name(args):
#code
Function call:
fun_name()
#sum of two numbers
def addition():
a = int(input("Enter first num"))
b = int(input("Enter second num"))
c = a+b
print(c)
addition()
Function with args:-
- You can give the input as values to the function inside an arguments.
def multiply(a,b):
print(a*b)
x = int(input("Enter first num"))
y = int(input("Enter second num"))
multiply(x,y)
#function with return type
#Create simple add function and get two num
#from user and calculate the sum and return
#sum value in next function and check
#the sum value is between 50 to 100 or not?
def addition():
a = int(input("Enter first num"))
b = int(input("Enter second num"))
c = a+b
return c
def result():
res = addition()
if(res>=50 and res<=100):
print("yes")
else:
print("no")
result()
Task:-
1. Using recursion approach find the factorial value of given number
2. Anonymous
- Function without name
- Lambda keyword is used to create anonymous function
Syntax:-
res = lambda var : exp
result = lambda a,b : a+b
print(result(3,4))
def maxi():
a =
b =
if(a>b):
print(a)
else:
print(b)
maxi()
maxi = lambda a,b : a if(a>b) else b
print(maxi(14,50))
def evenodd():
a = int(input("Enter the number"))
if(a%2==0):
print("even")
else:
print("odd")
evenodd()
evenodd = lambda a : "even" if(a%2==0) else "odd"
x = int(input("Enter the number"))
print(evenodd(x))
def maxthree():
if(a>b and a>c):
print(a)
elif(b>c): #else + if
print(b)
else:
print(c)
maxthree()
res = lambda a,b,c : a if(a>b and a>c) else (b if(b>c) else c )
print(res(300,14,5))
Task:- Lambda
1. Find the given number is two digit or three digit or none of the above
res = lambda n : "two digit" if(n>=10 and n<=99) else ("three digit" if(n>=100 and
n<=999) else "none of the above" )
print(res(1000))
2. Find the given string is palindrome or not?
result = lambda s : "palindrome" if(s==s[::-1]) else "not a palindrome"
https://drive.google.com/drive/folders/1QgvRTK_fbaM2YJsvsKTwCPQi779SsFqu?
usp=drive_link
3. Pre defined function
- It is already defined in python lang.
- Import keyword is used to identify pre defined function
- It is also known as modules
Modules
Math - It contains mathematical functions. It is mostly used in machine learning,
AI. It is rarely used in basic python programming
Factorial - 0! = 1, 1! = 1, 2! = 2*1, 3!=3*2*1, 4!=4*3*2*1
import math
#print(dir(math))
#num = int(input("Enter the number"))
#print(math.factorial(num))
'''f = 1
#4
for i in range(1,num+1):
#f=1*1 = 1
#f=1*2 = 2
#f=2*3 = 6
#f=4*6 = 24
f = f*i
print("fact is: ",f)'''
'''l = [1,2,3,4,5,6]
a = int(input("Enter first number"))
b = int(input("Enter second number"))'''
#print(math.gcd(a,b))
#print(math.lcm(a,b))
#print(math.sqrt(num))
#find the fact value for all list values
'''for i in l:
print(math.factorial(i))'''
#Find the area of circle
r = int(input("Enter the radius"))
#print(math.pi*r*r)
print(math.sin(0))
print(math.cos(0))
print(math.tan(0))
print(math.log(10))
print(math.log(2))
print(math.sinh(10))
Task:-
l = [2,3,4,5]
Print the sqrt for all the elements in the list using math module
for i in l:
print(math.sqrt(i))
Print the power of all the elements in the list using math module
for i in l:
print(math.pow(i,3))
Random - It is used to generate random data. Random
password,otp,pin,coupon,capcha..
Methods
1. randrange
2. randint
Example:-
import random as r
#print(r.randrange(5)) #0 to 4(end-1)
#print(r.randrange(1,11)) #1 to 10
#print(r.randrange(1,11,3)) #1,4,7,10
#print(r.randint(1,10)) #1..10(end is also included)
#get a number from user(1..10)
#generate random num and compare both are same or not?
'''num = int(input("Enter the number"))
rand = r.randint(1,10)
if(num == rand):
print("correct guess")
else:
print("wrong guess")
ch = input("Do you want to reveal?")
if(ch=='yes'):
print(rand)'''
List of data
3. choice
4. shuffle
5. sample
import random as r
l = [12,13,14,15,16]
#print(r.choice(l))
#r.shuffle(l)
#print(r.sample(l,4))
Random OTP
- Add string module
import random as r
import string as s
#print(dir(s))
#print(s.ascii_letters)
#print(s.digits)
di = s.digits
#convert string to list
ls = list(di)
#take any six random digits
res = r.sample(ls,6)
#convert string to list
result = "".join(res)
print(result)
Random four lowercase alphabets
import random as r
import string as s
l = s.ascii_letters
#convert string to list
ls = list(l)
#taken random 4 lower case
res = r.sample(ls,4)
#join the list value into res
result = "".join(res)
print(result)
Task:-
Generate two upper,three lower and one digit
Re
NumPy - pip install <mod-name>
Pandas
Django