Show Menu
Cheatography

Python Sheet Cheat Sheet by

Words

Variable
The inform­ation that can change
String
The list of letters, numbers, symboles
Syntax
The computer's grammar
Boolean
The inform­ation that contains true or false
Modulo
Find the remainder

Math Operations

==
equal to
3 == 3
!=
not equal to
3 != 2
+
plus
3 + 2 = 5
-
minus
3 - 2 = 1
*
times
3 * 2 = 6
/
divide
9 / 3 = 3
%
the remainder from divide numbers
3 % 2 = 1
**
power
3 ** 2 = 9
<
less than
2 < 3
>
more than
3 > 2
<=
less than or equal to
2 <= 3 , 3 <= 3
>=
more than or equal to
3 >= 2 , 3 >= 3
//
divide by not included decimal point
2//2000 = 0

Calculate the Area of The Circle using def()

def areaofcircle(r):
    pi = 3.1415
    area = pi  (r * 2)
    return area

user_radius = float(input("Enter the Radius: "))
print("The area of the circle is ", areaofcircle(user_radius))

How To Reverse

word = input("Enter the word: ")
index = 0
reverse = ''
while index < len(word):
    reverse = word[index] + reverse
    index = index + 1 

print ("Reverse: ", reverse)

Calcul­ating Fibonacci

fibonacci = [0,1]
print(0)
print(1)
while len(fibonacci) < 50:
    number = fibonacci[len(fibonacci) - 2] + fibonacci[len(fibonacci) - 1]
    fibonacci.extend([number])
    print(number)

Code & Functions

int()
convert the value into integer with no decimal place
print()
print the value
float()
change the value into number with decimal point
input()
use for want the user to type text in
len()
use for count the string
str()
change the value into string
import
import the code into the list
#
things after # will not define as a code
if/else
things after 'if' is the code that works when the variable is in the condition. If not, the code in the 'else' code will be worked.
elif
to define that there has an 'if/else' code in the other 'else' code
while
the code in 'while' section will be repeated along to the condition
 

How To Create A List

def createlist(quitword):
    print ("Keep entering words to add to the list")
    print ("Quit when word =", quitword)
    mylist = []
    while True:
        user_word = input("Please ente0r a list item: ")
        if user_word == (quitword):
            return mylist
        duplicate = False
        for item in mylist:
            if item == user_word:
                duplicate = True
        if (duplicate == True):
            print ("Duplicate Word!")
        else:
            mylist.append(user_word)
userlist = createlist("stop")
print (userlist)

Check The Word is Palindrome or Not

def isPalindrome(user_word):
    length = len(user_word)
    while length >= 1:
        firstnumber = 0
        firstletter = user_word[firstnumber]
        lastletter = user_word[length - 1]
        if firstletter == lastletter:
            firstletter == firstnumber + 1
            lastletter == length - 1
            length = length - 2
            if length == 0 or 1:
                return True
        else:
            return False
                
        
print ("Keep entering words to check that the word is palindrome or not.")
print ("Quit when word = quit")
while True:
    user_word = input("Please enter a word: ")
    if user_word == ("quit"):
        break
    else:
        length = len(user_word)
        print ("The length of the word is:", length)
        if isPalindrome(user_word) == True:
            print (user_word, "is a palindrome")
        else:
            print (user_word, "is not a palindrome")

Guessing Game

import random
chance = 3
score = 0
while chance > 0:
    print ("Guessing game")
    mylist = ['bowling','badminton','table tennis','basketball','golf']
    print ("Words:" ,mylist)
    randomitem = random.choice(mylist)
    userguess = input("Please guess a word: ")
    if userguess == randomitem:
        score = score + 100
        print ("That's Correct! Score: ", score)
    elif userguess in mylist:
        chance = chance - 1
        print ("Sorry, wrong choice!")
        print("Chance: ", chance)
    else:
        chance = chance - 1 
        print ("Sorry, that is not even in the list!")
        print("Chance: ", chance)
if chance == 0:
    print ("Game Over! The word was ", randomitem)
    print ("Final Score:", score)
 

Convert Integer into Binary

integer = input("Enter number: ")
integer = int(integer)
remainder = integer
binary = ''
while integer != 0:
    remainder = integer % 2
    integer = int(integer / 2)
    remainderstr = str(remainder)
    binary = binary + remainderstr
if integer == 0:
    index = 0
    binary2 = ''
    while index < len(binary):
        binary2 = binary[index] + binary2
        index = index + 1
    print(binary2)

Math Operations

string + string
combine string together
("Stop ") + ("Wo­rki­ng") = ("Stop Workin­g")
string + number
crash
number + number
add numbers together
(3) + (2) = (5)
string * string
crash
string * number
combine string many times
("I") * (3) = ("II­I")
number * number
multiply numbers
(3) * (2) = (6)
string ** string
crash
string ** number
crash
number ** number
math exponents
(3) ** (2) = (9)

Loop & def()

#forloop
forlist = [1,2,3]
for item in forlist:
    print(item)
    
#whileloop
whilelist = [1,2,3]
whilelen = 0
while whilelen != len(whilelist):
    print(whilelist[whilelen])
    whilelen = whilelen + 1


#show the length of the giving word
print ("Keep entering words to add to the list")
print ("Quit when word = exit")
while True:
    user_word = input("Please enter a list item: ")
    if user_word == ("exit"):
        break
    else:
        length = len(user_word)
        print ("The length of the words is", length, ".")

#type the words in using loop
def theFunction():
    print ("Keep entering words to add to the list")
    print ("Quit when word = stop")
    user_word = input("Please enter a list item: ")
    while True:
        if user_word == ("stop"):
            break
        else:
            user_word = input("Please enter a list item: ")
    return
            
theFunction()

#times the number using def()
def computethis(a1,b2):
    compute = a1 * b2
    return compute
a1 = int(input('First Number: '))
b2 = int(input('Second Number: '))
print(computethis(a1,b2))

#add stars to the word
def finalFunction(string):
    star = '*' + string + '*'
    print(star)
    return
finalFunction("777")
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          More Cheat Sheets by pca221