Show Menu

Nancy's Python Cheat Sheet by


Naming Convention

Rule for giving name
- letter
- numbers
- underscore _
Valid name
- _myStr
- my3
- Hello_­​there Invalid name
- 3my=”hi” -- cannot start with number
- first name=”hi”
- first-name
- first+name


Not equal
More than or equal
Less than or equal
% (Madulo)
Find the remainder

Multip­­li­c­ation and Exponents

string * number
Combine that string
string* string
number * number
Multiply (Math)
string ** string
number ** number
Exponent (Math)
string ** number

Area of Triangle

def areaOfTriangle(base, hight) :
    return 0.5 base hight

user_base = float(input("Enter the base of the triangle: "))
user_height = float(input("Enter the hight of the triangle: "))

print ("The area of the triangle is", areaOfTriangle(user_base, user_height))

def volumeOfPrism(area, high) :
    return area * high

user_prism_high = float(input("Enter the hight of the prism: "))

print ("The volume of the prism is", volumeOfPrism(areaOfTriangle(user_base, user_height), user_prism_high))

Reverse Word

while 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)

Sort fruit list

fruits = [] #an empty list

for number in range(5):
    user_fruit = input("Please enter a fruit")

print ("Size of fruit list is", len(fruits))


for fruit in fruits:
    print ("Fruit: ", fruit)

Countdown Machine

number = int(input("What number do you want to count down? "))
countdown_string = ' '

while number > 0:
    countdown_number = countdown_string + str(number) + " "
    number = number - 1
print (countdown_string)

Area of Circle

user_radius = input("What is a radius of a circle?")

radius = float(user_radius)

pi = float(3.1415)

area = pi(radius*2)

print ("The area of the circle is", area)

Create List

create a function name: createList
argument: quitword
return: a list

def createList(quitword):
    mylist = [] #empty list

    while True: #loop forever
        user_word = input("Please enter a list item: ")

        if user_word == quitword:
            return mylist #returns the list and exits the function

        duplicateword = False

        for item in mylist:
            if user_word == item:
                duplicateword = True

        if (duplicateword == True):
            print ("Duplicate Word!")
            mylist.append(user_word) #Adds the user_word to the end of the list
userlist = createList('stop') #Function Call
print (userlist)

Function (Ex.)

def myprint2(text, decoration): #Text and decoration is a parameter
    print (decoration + str(text) + decoration)

myprint2("Hello", "+++++")
myprint2("Hello", "-=-=-=-=")
myprint2("Hello", "<<<<<")


def isPalindrome(word):

    index = 0
    reverse = ''

    while int(index) < len(user_word):
        reverse = user_word[index] + (reverse)
        index = int(index) + 1

    if user_word == reverse:
        return True
        return False

while True:
    user_word = input("Please enter a word: ")

    if user_word == "quit":
    print("Length of the", user_word, "is", len(user_word))
    Palindrome = isPalindrome(user_word)

    if Palindrome == True:
        print (user_word, "is a Palindrome!")
        print (user_word, "is not a Palindrome")

Sort word per line

mystr = "Hello"

letter_num = 0

while letter_num < len(mystr):
    print (mystr[letter_num])
    letter_num = letter_num + 1

Convert decimal to binary

user_number = ' '

while user_number != ' 0 ' :
    user_number = input ("Enter a number to convert to binary")
    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)

Area of Circle by Function

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

user_radius = input("Enter the radius of the circle: ")
radius = float(user_radius)

print("The area of the circle is", areaOfCircle(radius))

Guess word game

import random

guesslist = ['grape', 'orange', 'chloroplast', 'ribosome', 'lipstick']

chance = 3
score = 0

print (guesslist)

while chance != 0:
    random_item = random.choice(guesslist)
    user_input = input("Please guess a word: ")
    if user_input == random_item:
        print ("That's correct!")
        score = score + 100
        print ("Score:", score)
        if user_input not in guesslist:
            print ("Sorry, that isn't even in the list!")
            chance = chance - 1
            print ("Chance Remaining:", chance)
            print ("Sorry, wrong choice!")
            chance = chance - 1
            print ("Chance Remaining:", chance)

if chance == 0:
    print ("The word was", random_item)
    print ("The score is", score)

Convert binary to decimal

num = str(int(input("Enter a binary to convert to decimal ")))

dec = int(num, 2)

print ("The decimal number is", dec)

Download the Nancy's Python Cheat Sheet

3 Pages

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

Like this cheat sheet? Check out our sponsors! is a collection of tools to make your writing better. More readable content means higher conversion rates and better reader engagement. Measure website and document readability, measure keyword density and more!

Click Here To Get Started!



No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Web Programming Cheat Sheet
          Essential Python Cheat Sheet