Function
int = integer str = string len = length Statements
elif = else if Circle area#Ask the user for a radius of a circle user_radius = input("What is a radius of a circle?") #Convert the given radius to a floating point radius = float(user_radius) #Make a variable called pi pi = float(3.1415) #Calculate the area of the circle using exponents area = pi(radius*2) #Display the area of the circle to the user print ("The area of the circle is", area) Guess word game
print ("That's correct!") score = score + 100 print ("Score:", score) else: if user_input not in guesslist: print ("Sorry, that isn't even in the list!") chance = chance - 1 print ("Chance Remaining:", chance) else: print ("Wrong choice!") chance = chance - 1 print ("Chances:", chance) if chance == 0: print ("The word is", random_item) print ("Your score", score) Triangle
base = float(input(" Enter the base of the triangle")) height = float(input(" Enter the height of the triangle")) print ("The area of the triangle is", areatriangle(base,height)) def volumeprism(area,height): volume= area*height return volume prism_height = float(input("Enter the height of the prism")) print("The volume of the prism is", volumeprism(areatriangle(base,height),prism_height)) radiusdef area_circle(r): pi=3.1415 area= pi r * 2 return area radius= input("Enter the radius of the circle") radius=float(radius) print("The area of the circle is", area_circle(radius)) |
Symbol
= is only for variable not for doing math Symbol II
Reversewhile True: word = input("Please enter a word") index = 0 reverse = ' ' while int(index) < len(word): reverse = word[index] + (reverse) index = int(index) + 1 print ("Reverse: ", reverse) Countdownnumber = int(input("Write a number,I'll countdown to one.")) while number>0 : print(number) number = number-1 sort word
mystr = "Hello" letter_num = 0 while letter_num < len(mystr): print (mystr[letter_num]) letter_num = letter_num + 1 Calculator
def main(num1,num2,op): if (op == "sum"): print(sum(num1,num2)) elif (op == "difference"): print(diff(num1,num2)) elif (op == "product"): print(mult(num1,num2)) elif (op == "division"): print(div(num1,num2)) functiondef bacon(text): # text is a parameter, pararmeter is what u give to the function print ("" + str(text)+ "") return bacon("me") bacon("yay") def bacon2(text,decoration): print (decoration +str(text) +decoration) return bacon2("hello", "++++++++") bacon2("me", "-=-=-=-=-=-=-") bacon2("<3", "<<<<<<<<<<<<<") bacon2("hello") # miss decoration double itdef doubleIt (number): # dont have to call it doubleiT, CALL WHATEVER U WANT return number*2 print (doubleIt(2)) print (doubleIt("Hello")) myvar = doubleIt(doubleIt(3)) #same as doubleIt(6) print(myvar) |
Vocabulary
Random
import = put the program(somebody wrote it) in Convert to binarynumb = int(input("Write down another number,I'll turn it into binary.")) binary_string = '' while numb>0: print (numb) remainder = numb%2 binary_string = str(remainder)+ binary_string numb= numb//2 print ("The binary number is",binary_string) Print Namename = "tim GIRARD" print (name.upper()) → TIM GIRARD print (name.lower()) → tim girard print (name.capitalize()) → Tim girard print (name.title()) → Tim Girard sort listSort fruit list fruits = [] #an empty list for number in range(5): user_fruit = input("Please enter a fruit") fruits.append(user_fruit) print ("Size of fruit list is", len(fruits)) fruits.sort() for fruit in fruits: print ("Fruit: ", fruit) Palindrome
index = 0 #set to 0 because that is the first item in the list while int(index)<len(word): reverse=word[index] + (reverse) index=int(index)+1 Palindrome = False print ("|" + reverse + "|" + word +"|") def isPalindrome(word): if word == reverse: print (True, ",",word ,"is a palindrome") else: print(False, ",",word ,"is not a palindrome") palindrome = isPalindrome(word) print in seperated linemylist=[1,2,3,4,5] for number in mylist: print(number) |
Cheatography
https://cheatography.com
Python note! Cheat Sheet by dragonjeen
mine
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment