Posts

Showing posts from February, 2021

Reversing a string and checking if it's a palindrome

 str='Joke'.upper() reverse_string="" count=len(str) while count>0:     reverse_string+=str[count-1]     count=count-1 print(f'Reverse string is {reverse_string}') if reverse_string==str:     print("It's a palindrome") else:     print('not a palindrome')

Car Game

 command="" started=False while True:     command = input('>').lower()     if command=='help':         print(""" start- to start the car stop- to stop the car quit- to exit """)     elif command=='start':         if started:             print('car already started')         else:             started=True             print('Car started!')     elif command=='stop':         if not started:             print('Car already stopped')         else:             started=False             print('car stopped')     elif command=='quit':         break     else:         print('do not understand')

Print {x,y} in required fashion!

 for x in range(4):     for y in range(3):         print(f"Coordinates are {x},{y}")

Sum of shopping cart price!

prices=[10,20,30,40] total=0 for price in prices:     total+= price print(total)

Guess game!

secret_number=9 i=1 while i<=3:     b=int(input('Guess: '))     i+=1     if b==secret_number:         print('You won!')         break else:      print('You failed!')

Weight Converter from pounds to kilos and vice versa

 Weight=input('Weight: ' ) Type= input('(L)bs or (K)g: ') if Type=='L' or Type=='l':     print(f'You are {int(Weight)*0.45} Kilos') if Type=='K' or Type=='k':     print(f'You are {int(Weight)/0.45} pounds')

Name length entering as per conditions

 Name= input("What is your name? ") Name_Length= len(Name) if Name_Length<3:     print('Name must be at least 3 characters') elif Name_Length>50:     print('Name is too long') else:     print('Name looks good')

Loan approval program for someone with criminal record

 has_good_credit=True has_criminal_record=False if has_good_credit and not has_criminal_record:#     print('Loan approved!') else:     print('Loan not approved') # only when the above 2 statements are true can u approve the loan will be approved has_good_credit is true "not has_criminal_record" is true 2 conditions satisfied  and hence loan will be approved 

Program to check whether loan is approved as per conditions

 has_high_income=True has_good_credit=False if has_high_income or has_good_credit: # OR is used when either of the condition is required to be true     print('Loan approved!') else:     print('Loan not approved') # here loan is approved as one of the condition is correct

Program to calculate down payment of house

 price=1000000 has_good_credit=True if has_good_credit:     print('put down 10%')     DP=.1*price else:     print('put down 20%')     DP=.2*price print(f'Your Down payment is ${DP} only')

Use of methods and boolean operations

Course = 'Python for beginners' print(len(Course))  #print length print(Course.upper())  # print in capital print(Course.lower())  # print in lower case # upper and lower are methods print(Course.replace('beginners','Absolute Beginners')) # replace a word with another print(Course.find('P'))          # to find index of alphabet print('for' in Course)   # boolean operation only comes in true or false # Here upper is a method # Len and print are general purpose functions but methods are specifically use for strings

How to concatenate strings

 First = 'John' Last = 'Smith' message = First + ' [' + Last + '] ' + 'is a coder' msg = f'{First} [{Last}] is a coder' print(message) print(msg) # f represents format # printing message will be difficult to visualize so we used format option #First and Last are put in curly brackets while using f option

Convert Weight in pounds to Kilograms!

 Weight_Pounds= input('What is your weight in pounds? ') print(int(Weight_Pounds)*.453)

Find age from birth year

 Birth_Year = input('What is your birth year? ') Age =2021-int(Birth_Year) print(type(Birth_Year)) print(Age)

Take input of name and color and print in a pattern

 Name = input('what is your name?' ) Color = input('What is your faovourite color?' ) print (Name + ' likes ' + Color)