Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Python core/loops.ipynb
3074 views
Kernel: Python 3

If else loops & Functions

image.png

CONDITIONAL Operators

'''a = [3,4,5] b = [3,4,5] x=4 print("x==2 ::" ,x==2) print("x==3 ::" ,x==3) print("x>3 ::" ,x>3) print("x<3 ::",x<3) print(" Use of '=' OPERATOR :", a==b) print(" Use of 'is' OPERATOR :" , a is b) print(not False) print((not False)== (False))''' print("Boolean operations:") name = "ashi" age = 26 if name == "ashi" and age == 26: print("Your name is ASHI & you are 26 years old") if name == "ashi" or name == "shobhi": print("Your name is either Ashi or SHOBHI") print(" Use of 'in' operator") if name in ["ashi", "Shobhi"]: print("Your name is either ashi or shobhi.") if x==2: print("x is equal to two") else: print("x not matches ")

IF ELSE

WAP to check length of a string and if len>9 ,print("L") ,if it is 9 to 5 print("M") else ("S")
x=int(input("Enter num")) if x==0: print("x is null") elif x>0: print("positive") else: print("negative")
x=input("Enter user name") if x.isdigit(): print("String has digits") elif x.isalpha(): if x.isupper(): print("Uppercase") elif x.islower(): print("lowercase") else: print("Upper case & lowercase combination") else: print("Mixed")
x=9 if x>5: print("Hello") else: print ("BYE")

LOOPS

for x in range(21,10,-2): print(x)
for i in range(21,30,2): print(i)
for i in {1:6,10:15}: print(i)
for i in range(12,6,-2): print(i)
for i in range(30,100,2): print(i)
for i in range(9,1,-2):#(start,stop,difference) print(i)
a="python" b=(1,2,"a") c=[1.2,3,"x"] d={"one":1,"two":2} for i in d: print(i)
a=[1,2,"ashi"] #print(len(a)) for i in a: print(a)
# iterating String using for loop: a=[1,2,3,4,5] for i in a: # loop will hold the range = len(a) print(a)
#input list l=[] number = int(input("how many value you want in a list: ")) for i in range(0,number): values = int(input("enter member:")) l.append(values) print(l)
how many value you want in a list: 4 enter member:-3 enter member:444 enter member:56 enter member:87 [-3, 444, 56, 87]
##Create a dictionary for emp ID as key and jobrole as value.
#Code to generate & print dictonary n=int(input("Enter no of students")) d = {} for x in range(1,n+1): i=int(input("Enter roll num")) name=input("Enter your name") d[i]=name #value is square of number print(d) print("*"*20)
Enter no of students3 Enter roll num1 Enter your nameram Enter roll num2 Enter your nameash Enter roll num3 Enter your namesid {1: 'ram', 2: 'ash', 3: 'sid'} ********************
  • Ques: Validity of Password factors: one uppercase ,lowercase,digit and spl character,nospace.len(8-20) hint="@#%&*_>>"

pwd=input('enter passowrd') d=a=u=s=l=sp=0 w="@#$%^&*_><?/" for c in pwd: if c.isdigit(): d=d+1 elif c.isalpha(): a=a+1 if c.isupper(): u=u+1 elif c.islower(): l=l+1 elif c in w: s=s+1 elif c.isspace(): sp=sp+1 else: pass if d==0: print("invalid password no digits") if sp>0: print("Invalid space not allowed") elif a==0: print("INVALID ENTER SOME LETTERS") elif u==0: print("INVALID ENTER one in uppercase") elif l==0: print("INVALID ENTER one in lowercase") elif s==0: print("INVALID enter special character") elif len(pwd)<10: print("INVALID MINIMUM LENGTH SHOULD BE 9") else: print("Valid pasword")
enter passowrda1B@ INVALID MINIMUM LENGTH SHOULD BE 9
list=[] number = int(input("how many value you want in a list: ")) for i in range(0,number): element = int(input("enter your choice number:")) list.append(element) print(list)
x=0 # while x<4: print(x) x=x+1 print("end")
0 1 2 3 end
i=0 #initialization while i<5:#condition via while loop x=input("Enter your name") i=i+1 print("end")

Ques: Count even & Odd from a list

numbers = [22,6,1,3,5,5,6,8,9,12,13,11,11,13,13] # Declaring the list count_odd = 0 count_even = 0 for x in numbers: if not x % 2: count_even+=1 else: count_odd+=1 print("Number of even numbers :",count_even) print("Number of odd numbers :",count_odd)
#Code to generate & print dictonary n=int(input("Input a number ")) d = {} for x in range(1,n+1): d[x]=x*x #value is square of number print(d) print("*"*20)

Functions

def demo(x,y): return(x) demo(3,8) demo(6,12.5) demo("py","thon")
def Emp(name="",age=10): x=input("Enter your name") y=int(input("Enter your age")) #z=float(input("Enter your year of experience")) #return ; Emp() Emp()
def add(a,b): x=a+b print(x) add(2,7)
def mul(x,y): prod=x*y print("The product inside=",prod) return x*y print("The product outside=",mul(9,11),)
def data(name="",roll=00): print("%s \t rollno= %d"%(name,roll)) data("ASHI",2) data("RAM",5)
n=int(input("Enter number")) for i in range (1,11): print(n,'x',i,'=',n*i)
#Map two list into dictionaries keys = ['red', 'green', 'blue'] values = [0,1,2] d1 = dict(zip(keys, values)) print(d1) print("*"*20)
import operator d = {1: 1, 2: 4, 5: 25, 4: 16} sorted_d = sorted(d.items(), key=operator.itemgetter(0)) print('Dictionary in ascending order by value : ',sorted_d) sorted_d = sorted(d.items(), key=operator.itemgetter(0),reverse=True) print('Dictionary in descending order by value : ',sorted_d) print("*"*20) #add a key to a dictionary. d = {0:10, 1:20,3:30} d.update({2:40}) print(d) print("*"*20) for d in (dic1, dic2, dic3): dic4.update(d) print(dic4) print("*"*20) #Sum all values in a dict my_d = {'d1':100,'d2':-4,'d3':12} print( "sum=",sum(my_d.values())) print("*"*20) #iterate dictinary over loop d = {'name' : 00, 'ash': 1, 'ashi': 2, 'sid': 3} for dict_key, dict_value in d.items(): print(dict_key,':',dict_value) print("*"*20) #Map two list into dictionaries keys = ['red', 'green', 'blue'] values = [0,1,2] d1 = dict(zip(keys, values)) print(d1) print("*"*20)
keys = ['red', 'green', 'blue'] values = [0,1,2] d1 = dict(zip(keys, values)) print(d1) print("*"*20)