Cheatography

# Python Cheat Sheet by poohiez

Python

### Function

 print() PRINT an inform­ation on the screen input() ASK for an inform­ation str() Change an inform­ation into a STRING int() Change an inform­ation into a INTEGER float() Change an inform­ation into a DECIMAL len() LENGTH of the string # Used to COMMENT what we've done ''' MULTI line COMMENT

### CODE: Countdown Number

 ``````user_number = input("Please enter a number: ") number = int(user_number) countdown_string = "" while number > 0:     countdown_string = countdown_string + " " + str(number)     number = number-1 print (countdown_string)``````

### CODE: Define Ex

 ``````def hello ():     print ("Hello it's Pooh")     return hello() Ans : Hello it's Pooh def myprint(text):     print ("" + str(text) + "")     return myprint(1) Ans : 1 def mynewprint(text,decor) :     print(decor + str(text) + decor)     return mynewprint(1, "+++") mynewprint(555, "+++") Ans : +++1+++ +++555+++ def doubleIt (number) :     return number * 2 print (doubleIt(5)) myvar = 12 myvar = doubleIt(myvar) print (myvar) Ans : 10, 24 def AreaOfCircle(r):     if r <= 0:         return "Error: invalid radius"     pi = 3.1415     area = (pi(r*2))     return area user_radius =input("Enter your radius: ") r = float(user_radius) print ("The area of your circle is", AreaOfCircle(r))``````

### Math

 == Equal to != Not equal to < Less than > Greater than <= Less than or Equal to >= More than or Equal to * Multiply ** Power (Exponent) / Divide (The ans. is in FLOAT form) // Divide (The ans. is in INTEGER form) % Modulo (Find the remainder)

### CODE: Reversing word

 ``````word = input("Type in an word: ") reverse = "" for letter in word:     reverse = letter + reverse print ("Reverse: ", reverse)``````

### CODE: Convert Int to Hexade­cimal

 ``````while True:     user_number = input("Please enter the number: ")     number = int(user_number)     binary_string = ''     while (number > 0):         remainder = number % 2         binary_string = str(remainder) + binary_string         number = number // 2     print("Binary string is", binary_string)``````

### CODE: Palindrome (Way1)

 ``````user_input = input("Type in your string: ") reverse = "" for letter in user_input:     reverse = letter + reverse print ("Reverse: ", reverse) palindrome = reverse if User_input == palindrome:     print ("Your input is palindrome") else:     print ("Your input is not palindrome")``````

### Math with STRING and INTEGER

 str + str Squishes them together int + int Do math (Add) int * int Do math (Multiply) int ** int Do math (Exponent) str * int combines the strings × number time str + int CRASH str * str CRASH str ** int CRASH

### CODE: Str, Fp, Int Random Ex

 ``````import random intlist = [1,2,3,4,5,50000000] random_list = random.choice(intlist) print(intlist, random_list) --------------------------------------- fplist = [1.1,1.2,1.3,1.4,1.5,5.000000] random_fp = random.choice(fplist) print(fplist, random_fp) strlist = ["one","two","three","four","five","ten"] random_str = random.choice(strlist) print(strlist, random_str) mylist = [1, 1.1, "one"] random_item = random.choice(mylist) print(mylist, random_item) myvar1 = 1 myvar2 = 2 myvar3 = 3 varlist = [myvar1, myvar2, myvar3] random_var = random.choice(varlist) print(varlist, random_var)``````