All Python Lernings
All Python Lernings
'''
mutable- list, dict,set
immutable-others
identifier = literal
print("hello",end ="@")
print("world")
'''
OUTPUT:
hello@world
'''
#**********************************************************************************
***********
#PYTHON STRINGS
#**********************************************************************************
***********
#PYTHON LISTS
'''
list.pop(index) --->pops the item at index
list.remove(item)--->pops the item
list.append--->adds item
list.extend---->adds list to list
list.index(item)---->RETURNS the index of the first occurence of the item
list.insert(index,item)--->ads item at index
list.reverse()
'''
#**********************************************************************************
***********
#PYTHON DICTIONARIES
dic = {
"name":"amaan",
"age":18,
}
name = dic["name"] #'amaan' will be stored in name
name = dic.get("name")
for x in dic:
print(x) #x is ekchuly the keys, therefore dic[x] is value
print(dic[x])
dic2 = dic.copy()
#**********************************************************************************
***********
# PYTHON FUNCTIONS
'''
if you pass mutable data types, it works as global var
if immutable, local var
EXAMPLE-
'''
a = 10 #int datatype-immutable
def func1(a):
a = 20 #working as a temp var for function not upar wla a
print(a) #we get changed value printed
print(a) #10
func(a) #20
print(a) #10
b = [1,2,3]
def func2(b):
b=[]
print(b)
print(b) #[1,2,3]
func(b) #[]
print(b) #[]
'''
global variable can also be set by using a keyword global
'''
def func():
global x
x = "oke"
func()
print(x)#'oke'
'''
default value of a function can be set by just doing var = something in arguments
EXAMPLE
'''
def func(a, b=90) #if we dont specify b, it will take 90
'''
if we take a mutable datatype in the default, it will be GLOBAL like b4
'''
def func(b,L[]):
L.append(b)
return L
print(func("a")) #["a"]
print(func("b")) #["a","b"]
print(func("c")) #["a","b","c"]
'''
if we dont want default value to be shared between subsequent calls
'''
def func(b,L=None):
if L is None:
L=[]
L.append(b)
return L
print(func("a")) #["a"]
print(func("b")) #["b"]
'''
you can use keyword arguments--> func(c=10,a =4, b = 3)
to escape the order i.e.-->func(4,3,10) (a,b,c)
'''
Variable number of arguments!
if u dont know how many arguments user will gib, u can use this, u get a tuple of
all extra arguments
EXAMPLE-
'''
def func(a, b, *extra_args): #extra_args will be a tuple wid all argument vals
print(a)
print(b)
print(extra_args)
func("a","b") #a b ()
func("a","b", 1, 2,3,4,5) #a b (1,2,3,4,5)
'''
agar arguments are in a list/tuple, we can unpaack by using "*"
EXAMPLE-
'''
def func(a,b,c,d):
return
lst = [1,2,3,4]
'''
lambda function is a pookie function and helps to do function inside function and
like vast
application shit
its only 1 expression
EXAMPLES:
'''
#1 intro
x = lambda a : a+10
print(x(5)) #15
#2 sex usage
def multiplier(n):
return lambda a : a * n
print(mydoubler(11))#22
print(mytripler(5)) #15
'''
documentation strings- strings used to explain the function to the user
EXAMPLE:
'''
def func():
'''THIS IS DOC STRING
THISS IS THAT
OKE
'''
pass
print(func.__doc__)
#output-
'''this is doc string
this is that
oke
'''
#**********************************************************************************
***********
#PYTHON MODULES-RANDOM:
import random
a = random.randrange(1,10)#1 and 10 not included
b = random.randint(1,10)#1 and 10 included
c = "abcd"
lst = ["a",1,'d',3]