0% found this document useful (0 votes)
0 views

All Python Lernings

The document provides an overview of Python programming concepts, including mutable and immutable data types, string manipulation methods, list and dictionary operations, and function definitions. It also covers the use of global variables, default arguments, variable-length arguments, lambda functions, and documentation strings. Additionally, it introduces the random module for generating random numbers and selecting random elements.

Uploaded by

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

All Python Lernings

The document provides an overview of Python programming concepts, including mutable and immutable data types, string manipulation methods, list and dictionary operations, and function definitions. It also covers the use of global variables, default arguments, variable-length arguments, lambda functions, and documentation strings. Additionally, it introduces the random module for generating random numbers and selecting random elements.

Uploaded by

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

#VIRGIN PYTHON

'''
mutable- list, dict,set
immutable-others

a//b gives gif of the answer

identifier = literal

keywords--->reserved words in python

a/b gives a decimal always


'''

#python print ends with a new line, it can be changed tho


print("hello")
print("world")
'''
OUTPUT:
hello
world
'''
print("hello",end =" ")
print("world")
'''
OUTPUT:
hello world
'''

print("hello",end ="@")
print("world")
'''
OUTPUT:
hello@world
'''

#**********************************************************************************
***********

#PYTHON STRINGS

var = string.capitalize() #makes first letter cap'


var = string.lower()#lowercase
var = string.upper()#uppercase
var = string.index(value) (OR) var = string.find() #tells index of value in string
var = string.count(value) #no. of occurences of value
var = string.isalpha() #var = True/False if the string is alphabet only or not
var = string.isalnum() #var = True/False if the string is alphanumeric not
#similarlyy----> isnumeric, isupper, islower,isspace
#to do all these operations on a part of the string: we use string INDEXING:
check= string[0].isupper()#0th index
check = string[0:3].islower()#0,1,2 index
check = string[0:5:2].isalpha#0,2,4 index therefore [] is basically range() inputs

var = string.split("smthin") #splits the string based off "smthin"


var = "smthin".join(list/tuple) #joins the items of a list or a tuple with
"smthin"
example:
lst = ["a", "b","c"]
word = "_".join(lst) #word => a_b_c
lst2 = word.split("_")#lst2 =>["a","b","c"]

#**********************************************************************************
***********

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

dic["name"] = "kauvya" #name will be changed


dic.update({"name":"kauvya"})

keylist = dic.keys()# keylist will be a list of all keys therefore here--->


["name","age"]
valuelist = dic.values()

dic.pop("name") #deletes name wala item


del dic["name"]

del dic #deletes dic


dic.clear()#empties dic

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)

u cant do keyword + non keyword, if u specify one ka then other ka too


'''

'''
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]

func(lst) #throws error


func(*lst) #unpacks the values

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

mydoubler = multiplier(2) #calling the multiplier(parent function)


mytripler = multiplier(3) #mydoubler and tripler stores another function which
needs arg-'a'

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]

print(random.choice(c)) #RETURNS a choice from string or list


print(random.choice(lst))

lst.shuffle()#shuffles list, no returning


#**********************************************************************************
***********

You might also like