The If-Else statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular condition evaluates to true.e.g.mood = input("how are you(good or bad)").lower()if(mood == "good") or (mood == "g"): print("Glad to hear")elif(mood == "bad") or (mood == "b"): print("Cheer up")else: print("answer me now")conditional statements: statements that give the user options and interactivity.lower()=automatically changes it to a lower case valuee.g. mood = input("how are you(good or bad)").lower().upper()=changes it to upper case\e.g.mood = input("how are you(good or bad)").upper()lists=used to store things in pythonDeclaring lists in python= e.g. cars=["off road","sport","muscle","Hybrids"]Using the list=for first one you have to 0 and then 1 then 2 then 3 etc.Calling the list=In order to call you must call it by it's index numbere.g. cars=["off road","sport cars","muscle cars","Hybrids"]cars[1]this is calling sport cars[random.randint()]=calls random things from list e.g.cars=["off road","sport cars","muscle cars","Hybrids"]print(cars[random.randint(0,3)])For loops: extremmly popular and often used to loop through lists; or through a rangee.g. cars=["off road","sport cars","muscle cars","Hybrids"]for name in cars: print("the types of cars are:"cars)range()=used to tell how many times the loop repeatswhile loops: a types of loop that directs it true or falsedecremeter: used to edit a variablee.g.10_car_garages=10while 10_car_garages > 0: 10_car_garages = 10_car_garages - 1time class= used to add a delaytime.sleep=used to sleepe.g. 10_car_garages=10while 10_car_garages > 0: 10_car_garages = 10_car_garages - 1 time.sleep(1) sleeps for 1 secondmaxLengthList=maxium number of things on a liste.g. maxLengthList = 10len()=the length of a compared variablee.g.cars = []maxLengthList = 10while len(cars) < maxLenghtList: item = input("choose a car")print(cars)break=break the loopitem= enters an item to a listappend()=The method append() appends(add something in an end of a written document) a passed obj into the existing list.e.g. cars.append(item)fuction syntax:def fuctionname (parameters): [fuction body] return[experesion]e.g.def cars(types): print("Play game?") print("the game has crashed")
Möchten Sie kostenlos Ihre eigenen Notizen mit GoConqr erstellen? Mehr erfahren.