'''age=18
if age>18:
print('you can vote')
elif age==18:
print('you can vote')
else:
print('you cannot vote')
a=5
b=6
c=a+b
print(c)
if 60>20:
print('true')
i=1
while i<6:
print(i)
if i==3:
break
i+=1
i=0
while i<6:
i+=1
if i==3:
continue
print(i)
a=33
b=230
if b>a:
pass
fruits= ["apple","banana","cherry"]
for x in fruits:
print(x)
fruits= ["apple","banana","cherry"]
for x in fruits:
print(x)
print(fruits)
for x in "banana":
print(x)
#range function
for x in range(6):
print(x)
for x in range(4,9):
print(x)
for x in range(4,10,2):
print(x)
colors=["red","black","yellow"]
fruits=["cherry","berry","mango"]
for x in colors:
for y in fruits:
print(x,y)
#functions
def my_function():#creating a function or function definition or called function
print("welcome to functions topic")
my_function()# calling function
def my_function(fname):
print(fname+" "+"berry")
my_function("red")
my_function("black")
my_function("blue")
# if no.of arguments is unknown ,add * before the parameter name
def my_function(*kid):
print("the youngest kid is "+kid[2])
my_function("hari","siri","giri")
#keyword arguments
def my_function(child3,child2,child1):
print("the youngest child is "+child3)
my_function(child1="hari",child2="siri",child3="giri")
#if no.of keyword arguments is unknown,add ** before the parameter name
def my_function(**kid):
print("the youngest child is "+kid["child1"])
my_function(child1="hari",child2="siri",child3="giri")
#default parameter value
def my_function(country="Norway"):
print("Iam from "+country)
my_function("India")
my_function("Sweden")
my_function()
my_function("paris")
#passing a list as an argument
def my_function(food):
for x in food:
print(x)
fruits=["apple","banana","cherry"]
my_function(fruits)
def my_function(x):
return 5 * x
print(my_function(2))
print(my_function(3))
print(my_function(4))
#lamba function
x=lambda a:a+5
print(x(2))
x=lambda a,b:a*b
print(x(4,5))
# lambda function in def
def myfunc(n):
return lambda a:a*n
mymul=myfunc(4)
print(mymul(2))
#map function
def addition(n):
return n+n
numbers=(1,2,3,4)
result = map(addition,numbers)
print(list(result))
#lambda expressions with map()
numbers=(1,2,3,4)
result=map(lambda x:x+x,numbers)
print(list(result))
#add two list using map and lambda
numbers1=[1,2,3,4]
numbers2=[1,2,3,4]
result=map(lambda x,y:x+y,numbers1,numbers2)
print(list(result))
#list of strings
strings=['hari','siri','nari','giri']
result=(map(list,strings))
print(list(result))
#arrays
phones=["vivo","samsung","nokia"]
print(type(phones))
print(phones)
phones=["vivo","samsung","nokia"]
phones.append("oneplus")
print(phones)
phones=["vivo","samsung","nokia"]
print(phones.pop(1))
phones=["vivo","samsung","nokia"]
phones.pop(1)
print(phones)
phones=["vivo","samsung","nokia"]
phones.remove("nokia")
print(phones)
phones=["vivo","samsung","nokia"]
print(len(phones))
phones=["vivo","samsung","nokia"]
phones[1]="oneplus"
print(phones)
phones=["vivo","samsung","nokia"]
for x in phones:
for y in x:
print(y)
x="matriculation"
for y in x:
print(y)
phones=["vivo","samsung","nokia"]
print(phones.index("vivo"))
phones=["vivo","samsung","nokia"]
phones.insert(1,"redmi")
print(phones)
phones=["vivo","samsung","nokia"]
phones.reverse()
print(phones)
fruits=["apple","banana","watermelon"]
for x in fruits:
print(fruits)
x="this is strings"
print(x[-5:-2])
print(x[-8:-3])
for x in "apple":
print(x)
txt="this is my calssroom where i am going to teach strings"
print("average" not in txt)
#string reversal
x="i am from"
print(x[::-1])
a="hello world"
print(a.upper())
print(a.lower())
print(id(a))
#list [ ]
#creating a list
#homogeneous may contain integers,strings,objects
#lists are mutable and hence they can be aaltered even after their creation
#unlike,sets a list does not need a built in function for its creation of a list
#list contain mutable elements but sets does not contain mutable elements
list=[1,2,3,"harika"]
print(list)
print(list[0])#accesing
print(list[-1])#negtive index accesing
#list may contain multiple distinct values or duplicants
list1=[1,2,3,3,3,4,4,6,5]
list2=[1,2,"harika",4,8,"harika"]
print(list1)
print(list2)
#accessing a element from multi dimensional list
list=[["geeks","for"],["geeks"]]
print(list[0][1])
#length of list
list=[1,2,3,4]
print(len(list))
#taking inputs of a python list
string=input("Enter elements : ")
list=string.split()
print(list)
#appending
list=[1,2,3,4]
list.append(5)
list.append(2)
print(list)
list.append((9,0))
print(list)
list1=[8,7]
list.append(list1)
print(list)#appending list to a list
for i in range(4,9)
list.append(i)
print(list)
#inserting
list=[1,2,3]
list.insert(0,9)# at 0 th index position we are adding element '9'
print(list)
#extend()
list=[1,2,3]
list.extend(['l','m','n'])
print(list)
#reversing a list
list=[1,2,3]
list.reverse()
print(list)
list1=[1,0,3]
print(list1[::-1])
#reversed function
my_list=[1,2,3,4]
reversed_list=list(reversed(my_list))
print(reversed_list)
#remove elements from a list
list=[1,2,3,4]
list.remove(3)
list.remove(4)
print(list)
list1=[1,2,3,4,5,6,7]
for i in range(1,4):
list1.remove(i)
print(list1)
#POP()
list=[1,2,3,4]
list.pop(0)
print(list)
#list comprehension
creating a new list from other iterables like tuple,strings,arrays,lists etc.,
syntax:
newlist=[expression(element) for element in old_list if condition]
odd_square=[x ** 2 for x in range(1,11) if x%2==1]
print(odd_square)
'''
#sets{}
#set is an unordered collection datatype that is iterable,mutable(we can remove or add elements)
and has no duplicants(once we created cannot change its value)
'''The major advantage of using a set,as opposed to a list, is that it has a highly optimised method for
checking whether a specific element is contained in
the set .This is based on Data structures known as hash table.Since sets are unordered,we cannot
access items using indexes as we do in lists
#sets are heterogeneous may contain integers,strings,boolean etc
var={1,2,"hari",7}
print(type(var))
print(var)
#type casting list to set
myset=set(["a","hari"])
print(myset)
#adding a element to set
myset.add(4)
print(myset)
#sets cannot have dupilcate values and are unique
myset={"a","b","c"}
print(myset)
myset[1]="d"
print(myset)#error we cannot chang values as they are unique but we can add or delete elements
#Frozen set are immutable we cannot add or remove elements in frozen set.we can use frozenset()
method for not adding or removing any values
myset=set([1,2,3,4])
print(myset)
frozen_set=frozenset([1,2,3,4,5])
print(frozen_set)
frozen_set.add(8)
print(frozen_set)
#sets are unordered
set1=set([1,1,1,2,2,2,3,3])
print(set1)#=>o/p {1,2,3}from a list
set2=set(["a","b","c"])
print(set2)#=>o/p{'b','a','c'}from a list
set3={1,1,'hari','hari',8.6,(1,2,3),"none"}
print(set3)#=>o/p{1,'hari',8.6,'none',(1,2,3)}
set4=set(('a','b','b','c','a'))
print(set4)#o/p{'a','b','c'}from a tuple
set4=set("harika")
print(set4)#o/p{'a','h','i','k','r'}
#adding elements to a set
people={"hari","siri","giri"}
print("people:",end=" ")
print(people)
people.add("nari")
print(people)
for i in range(1,7):
people.add(i)
print(people)
#union operation using union() method
people={"jaya","idrish","archil","arjun"}
vampires={"karan","arjun"}
population=people.union(vampires)
print(population)
#union using "|" operator
population=people|vampires
print(population)
#intersection operation using intersection() method
set1={1,2,3,4}
set2={3,4,5,6,7}
set3=set1.intersection(set2)
print(set3)
#intersection operation using "&" operater
set4=set1&set2
print(set4)
#difference of sets using difference() method
set1={1,2,3,4,5}
set2={3,4,5,6,7,8,9}
set3=set1.difference(set2)
print(set3)
#difference operation using "-" operater
set4=set1-set2
print(set4)
#clearing function in sets
set1={1,2,3,4,5}
print(set1)
set1.clear()
print(set1)
#remove() and discard()
set1={1,2,3}
set1.discard(1)
print(set1)
set2={5,6,7,8}
set2.remove(7)
print(set2)
#tuple ( )
tuple is immutable i.e,cannot be changed ,modified,added or removed ,it is an ordered sequence of
values.The values can be repeated but there number is always finite.
A tuple may contain mixer of integers,strings etc.,Each item in tuple has a unique position index
starting from "0".
Tuple are created using paranthesis where elements are separated using commas.
some tuples do not have paranthesis known as Tuple packing
tuple1=(1,2,3,"hari","a")
print(type(tuple1))
print(tuple1)
tuple2=()
print(tuple2)#empty tuple
list1=[1,2,3]
print(tuple(list1))
print(tuple('harika'))#tuple from string
#creating a tuple with nested tuples
tuple1=(1,2,3,4)
tuple2=("a","b","c")
tuple3=(tuple1,tuple2)
print(tuple3)
#creating a tuple with repetition
tuple1=('harika',)*3
print(tuple1)
#creating a tuple with use of loop
tuple1=("harika")
n=5
for i in range(int(n)):
tuple1=(tuple1,)
print(tuple1)
#accessing of tuple
tuple1=tuple("harika")
print(tuple1[0])
#tuple unpacking
tuple2=("sriram","is ","a","good","boy")
a,b,c,d,e=tuple2
print(a)
print(b)
print(c)
print(d)
print(e)
#concatenation of tuple
tuple1=(1,2,3,4)
tuple2=(5,6,7,8,9)
tuple3=tuple1+tuple2
print(tuple3)
#slicing a tuple
tuple1=tuple('miracles')
print(tuple1[1:])
print(tuple1[::-1])
print(tuple1[3:5])
#deleting a tuple
tuple1=(0,1,2,3)
del tuple1
print(tuple1)
#sort()function in lists
#sort() function is only used in lists
list1=[9,5,6,2]
print(list1.sort())#o/p=>none
print(list1)#o/p=>[2,5,6,9]
#sorting in a reverse way
list1.sort(reverse=True)
print(list1)#o/p=>[9,6,5,2]
#sort the list by key
def sortSecond(val):
return val[1]
mylist=[(1,2),(3,3),(1,1)]
mylist.sort(key=sortSecond)
print(mylist)
mylist.sort(key=sortSecond,reverse=True)
print(mylist)
#sorting characters
list2=["c","b","d","a"]
print(list2.sort())
print(list2)#o/p=>['a','b','c','d']
#sorted()
#sorted() function return a sorted list.It is not defined for list but also for any iterable.
list1=[3,5,4,1,2]
print(sorted(list1))#o/p[1,2,3,4,5]
print(list1)#o/p[3,5,4,1,2]values are not updated into list1 but in the case of sort()after applying
sort()function values are updated
print(sorted(list1,reverse=True))#o/p[5,4,3,2,1]
print(list1)
tuple1=('a','b','h','i','s')
print(sorted(tuple1))
string="harika"
print(sorted(string))
dictionary1={'q':1,'w':2,'e':3,'r':4,'t':5,'y':6}
print(sorted(dictionary1))
set1={'a','v','c','j'}
print(sorted(set1))
#reverse sorting using sorted()
string1="harika"
print(sorted(string1,reverse=True))
#Dictionary
A dictionary is a collection of key values.It holds key-value pair.Key-value is provided in the dictionary
to make it more optimised.
A dictionary is created by placing a sequence of elements within curly braces{}.Value in a dictionary
can be of any datatype and can be duplicated,whereas
keys can't be repeated and must be immutable.
Dictionary keys are case-sensitive,a same name with different keys will be treated distinctly
dict={1:'kanna',2:'is',3:'a',4:'good',5:'boy'}
print(type(dict))
print(dict)
dict1={1:'a',2:'a'} #same characters/same names/sames values with different keys are treated
distinctly
print(dict1)
dict2={'Name':'kanna',2:'is',3:'a',4:'good',5:'boy'}
print(dict2)
#creating a dictionary using dict()method
Dict={}
print(Dict)#empty dictionary
#nested dictionary
dict={1:'kanna',2:'is',3:'a',4:'good',5:'boy',6:{'a':'hari','b':'giri'}}
print(dict)
Dict2={}
print(Dict2)
Dict2[0]='harika'
Dict2[1]='hasini'
Dict2[2]=1
print(Dict2)
#adding set of values to a single key
Dict2['value_set']=2,3,4
print(Dict2)
#updating existing key's value
Dict2[1]='jyothi'
print(Dict2)
#Adding nested key values to a dictionary
Dict2[5]={'nested':{'1':'one','2':'life'}}
print(Dict2)
#accessing elements of a dictionary
dict={1:'kanna','a':'is',3:'a',4:'good',5:'boy'}
print(dict[1])#o/p 'kanna'
print(dict['a'])#o/p 'is'
#accessing a element using get()
dict={1:'kanna',2:'is',3:'a',4:'good',5:'boy'}
print(dict.get(5))
#accessing elements of a nested dictionary
Dict={'Dict1':{1:'harika'},'Dict2':{'Name':'hasini'}}
print(Dict['Dict1'])
print(Dict['Dict1'][1])
print(Dict['Dict2']['Name'])
#Deleteing elements using del keyword
dict={1:'kanna',2:'is',3:'a',4:'good',5:'boy'}
del(dict[4])
print(dict)
#pop()
dict={1:'kanna',2:'is',3:'a',4:'good',5:'boy'}
dict.pop(4)
print(dict)
#popitem()=>removes last key:value
dict={1:'kanna',2:'is',3:'a',4:'good',5:'boy'}
dict.popitem()
print(dict)
#update()
dict={1:'kanna',2:'is',3:'a',4:'good',5:'boy'}
dict.update({2:'was'})
print(dict)
'''