Show Menu

Python Sheet Cheat Sheet by

python

Operators

+
addition
-
subtra­ction
*
multip­lic­ation
/
division
//
divisi­on(­floor division)
**
exponent
%
module
==
equal to
!=
unequal to
<
lesser than
<=
lesser than or equal to
>
greater than
>=
greater than or equal to

Addition

string + string
combine together
string + number
crash
number­+number
math-a­ddition

Subtra­ction

String - String
crash
string­-number
crash
number­-number
math-s­ubt­raction

Multip­lic­ation

String * String
crash
String­*number
print string for number times
number­*number
math-m­ult­ipl­ication

using a while loop to print each item in list

wlist = [2,4,5,6,7,8]
index = 0
while index < len(wlist):
   print (wlist[index])
   index = index +1

Area of Circle Code

while True:
  user_radius = input("What is the radius?")
  radius = float(user_radius)
  pi = 3.1415
  area= pi radius * 2
  print ("The area of the circle is", area)

Area of a Triangle

def areaOfTriangle (base,height):
   return base heigh 0.5
base = float(input('Enter the base of the triangle'))
height = float(input(input('Enter the height of the triangle: '))
print('The area of the triangle is',areaOfTriangle(base,height))
def volumeOfPrism (area,height):
   return areaOfPrism* height
base = float(input('Enter the area of the prism'))
height = float(input('Enter the height of the prism: '))

Definition program code

def printDefinitions(word):
  if word == "variable":
    print( 'A variable is things that able to change')
  elif word == "function":
    print( "A function is to help to use a code")
  elif word == "variable":
    print( 'A variable is the things that help you to change')
  elif word == "return variable":
    print( 'A return variable is something that return the function back to you')
  elif word == "argument":
    print('A argument is something that give the function to you')
  elif word == "parameter":
    print('A parameter is something that give function')
  elif word == "string":
    print('A string is the text, number or anything that is list the characters')
  else:
    print('unknown word')
user_word = input( "Enter a word to define: ")
print(Definitions(user_word))
 

Function

print( )
displays inform­ation on the screen
input( )
receives inform­ation from the user
int( )
converts a value to an integer
float( )
converts a value to a decimal number
str( )
converts a value to a string
while... :
loop statement
if ... :
if statement used as a condition or loop in python
else :
another condition used after if statement
"­"­"
multi-line comment
#...
a line comment
for ... in ...
a list
True
a condition in a loop
False
a condition in a loop
len( )
length of the string
... [ x ]
the x'th letter of the string
import ...
import a codeor something like formula in python
random.ch­oic­e(...)
to random item from the list

Exponent

string­**s­tring
crash
string­**n­umber
crash
number­**n­umber
math-e­xponent

Division

String / String
crash
String­/number
crash
Number­/number
math-d­ivision

Palindrome

while True:
   def palindrome(word):
      reverse = ""
      myresult = ""
      for letters in word:
         reverse = letters + reverse
      if word == reverse :
         return True
     else:
         return False
     reverse = ""
  word = input("please enter a word: ")
  if word == "quit":
     break
  theresult = palindrome(word)
  print("This word has",len(word),"letter")

Random code

import random
mylist = ['Dog','Fish', 'Cat', 'Bear']
counter = 0
while counter < 10:
   random_item = random.choice(mylist)
   print (random_item)
   counter = counter + 1

Guessing Game code

import random
mylist = ['beagle','pomeranian','pug']
score = 0
chances = 3
start_over = 0
random_item =
random.choice(mylist)
while start_over < 1:
  print ("-=-=-=-=-=-=-=-=-=-=-=-")
  print ("Guessing Game")
  print ("-=-=-=-=-=-=-=-=-=-=-=-")
  print("words:", mylist)
  guess = input("Guess a word: ")
  if (guess in mylist):
     if(guess == random_item ):
        print("That's correct!")
        score = score + 100
        print("Score:", score)
        start_over = 2
     else:
       print("Sorry, wrong choice! ")
       chances = int(chances) -1
  else:
      print("Sorry, that is not even in the list")
      chances = int(chances) -1
  if(chances > 0):
      print("Chances remaining:",chances)
  else:
     start_over = 2
     print("Game Over! The word was ", random_item)
     print("Chance remaining:", chances)
print("Final score:", score)
 

Vocabulary

variable
holds a value and can be changed
string
a list of characters such as numbers, letters, symbols
floating number
number with a decimal point
integer
number with no decimal point
input
something that the user types in
syntax
grammar or rules on progra­mming
loop
the condition used in python
operator
the signs used for mathem­atics condition
module
text for storing the python code

Change the text

( ... . upper( ) )
change the text to upper case
( ... .lower( ) )
change the text to lower case
( ... . capita­lize( ) )
change the first letter of the text to upper case and convert other letters to lower case
( ... . title( ) )
change the first letter of each word from the text to upper case and convert other letter to lower case

print number in separate line in list mylist

mylist = [1,2,3,4,5]
for number in mylist:
   print (number)

number to binary code

user_number = ""
while user_number != "0":
   user_number = input ( "enter a number" )
   number = int(user_number)
  binary_string = ""
while (number > 0 ):#the number is greater than 0
   remainder = number % 2
   binary_string = str( remainder)+ binary_string
   number = number//2
   print (number)
print ( "binary string is ",
binary_string )

Count down code

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

Number printing( for loop)

for number in range(5):
print (number)

# the output will be 0-4 in separate lines

Quit word (def code)

# create a function that allows a user to create a list
#function name: word
#paramater: word
#return the list
def createList (quitword):
   mylist = [ ] #create an empty list
   while True:
#get the item from the user
    item = input('Please enter a list item')
# when the user enters an item that is equal to quitword
    if item == quitword:
        return mylist
# check if the list already in the list
    duplicateword = False
# figure out if the word isalready in the list
    for word in mylist:
       if item == word:
             duplicateword = True
       if duplicateword == True:
             print ('Duplicate word!')
       else:
# add this item to the end of the list
            mylist.append(item)
#function call
mylist = creatList("stop")
print(mylist)

Download the Python Sheet Cheat Sheet

3 Pages
//media.cheatography.com/storage/thumb/salisa-stamp_python-sheet.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

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.