Show Menu
Cheatography

Mimi's python skills Cheat Sheet by

Revision keywords&methods

Info4

Functions
#function with no parame­ter­s/a­rgu­ments
#and no return value
#return is optional if you do not return a value
def nameOf­Fun­cti­on():
print (‘This function has no parame­ters’)
print (‘This function has no return value’)
return # no value, just exits the function

#function call
nameOf­Fun­ction()

#function with 1 parame­ter­/ar­gument
def testFu­nct­ion­(pa­ram):
print (‘This function has 1 parame­ter’)
print (param)

#function call
testFu­nction (“this is the parameter value”)

#function with 2 parameters and a return value
def functi­on3­(pa­ram1, param2):
print(­‘This function has 2 parame­ters’)
return param1 + param2 # return value

#function call and store the result in a variable
return­Value = functi­on3(2, 3)
print (retur­nValue)

info3

Lists:
mylist = [2,3,4,5] # create a list
#select an item from a list
print (mylis­t[0]) #selects first item and displays 2
# len() determines the length of the list
print (len(m­ylist)) # displays 4
mylist.ap­pend(5) # adds an item to the end of the list

While Loop with List:
thelist = [4, 3, 2, 1, 0]
index = 0 # start at the first item
while index < len(th­elist):
print (theli­st[­index]) #prints each item
index = index + 1

For‐Loop with List:
forlist = [3, 4, 5, 2, 1]
for item in forlist:
print(­item)

Range()
#creates a list of numbers from 0 to the specified
number
numberlist = range(5)
# is the same as creating the following list
number­list2 = [0, 1, 2, 3, 4]
for num in range(­100):
print (num) # prints all numbers from 0 – 99
for num in range(5, 50):
print(num) #prints all numbers from 5 - 49

Python­4-M­ethods

#Mill's method
word= input("Please enter yout word")
index= len(word)-1
reverse= ''
while  (index>-1):
    reverse=reverse+word[index]
    index=index-1
print (reverse)

#mr's method
word= input("Please enter yout word")
index=0
reverse=''
while index< len(word):
    reverse=word[index]+ reverse
    index=index+1
print("reverse: ",reverse)

Python6

import random 

#Create a list

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)
    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 ("Sorry, wrong choice!")
            chance = chance - 1
            print ("Chance Remaining:", chance)

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

Keywords

print()
Show inform­­­ation that you want on the screen
int()
Change number to be number integer
float()
Change number to be decimal number
input()
Gain inform­­­ation from user
str()
A list of number, letter and symbols
len()
The length of the string
#
Comment, no effect
import random + random.ch­­oice()
pick random item in the list
==
equal to
!=
no equal to
<
less than
>
more than
<=
less than or equal
>=
more than or equal
%
Modulo, Find the remainder
string + string
combine together
string + number
CRASH
number + number
addition (Math)
string * number
combine that string
string* string
CRASH
number * number
Multiply (Math)
number ** number
Exponent (Math)
string ** number
CRASH
Variable
Hold a value and can be change
String
A list of character such as number, letter and symbols
Integer number
Whole number­­/c­o­u­nting number
Floating point
The number in decimal

convert dec num into its Binary form

number = int(in­put­("Enter number: "))
binary = " "
while number> 0:
remainder = number % 2
binary = str(re­mai­nder) + binary
number= number//2
print(­binary)

determine whether user inout is pos or neg num

number = int(input("Enter number: "))
   if number>0:
    print(number, "is positive")
   print(number,"is negative")

Python­1-M­ethods

"""
Python Intro Assignment #2
name
student number
"""

#Ask the user for a radius of a circle
user_radius =(input("What is the radius?"))
#Convert the given radius to a floating point
radius= float(user_radius)
#make a variable called pi
pi = 3.1415
#Calculate the area of the circle using exponents
area =(pi(radius*2))
#diaplay the area of the circle to the user
print("The area of the circle is", area)

ask user for input

mylist = [ ]
for number in range(5):
mylist.append(input("Enter value: "))
Ask the user fro input 5 items and add the values t a list called mylist, then print the list

largest value

number= [3, 2, 77, 32, 9, 8, 31]
   largest = 0
  for value in number:
  if number> largest:
    largest = number
   print (largest)
Determine the largest value from a given list
 

func take radius­,give back a of circle A=pi r*r

def AreaOfCircle(radius):
A=3.14radiusradius
return A
num= int(input("Enter a radius: "))
  x= AreaOfCircle(num)
   print(x)

info2

Basic Math Operat­ions:
+ addition, - subtra­ction
/ divide with answer as a float. E.g. 5/2 == 2.5
// divide with answer as an integer. E.g. 5//2 == 2
* multiply
exponent. E.g. 2 power 3 == 2 3
% modulo. Gives the remainder when dividing
e.g. 33 % 10 == 3
All math operations use the same order of operations as
Math class.

Comparing Values:
When you compare two values, the result is a Boolean
(True or False) E.g. 2 == 3 is False
 == is equal to
 != is not equal to
 < less than
 <= less than or equal to
 > greater than
 >= greater than or equal to
 and
 or
 not
True or anything is always True
False and anything is always False

Forever While Loop
while True: # forever
user_input = input(­'Enter a number: ')
number = int(us­er_­input)
print ('The number squared is', number ** 2)

Condit­ional While Loop:
count = 0 # start at zero
while count < 10: # loop while count is less than 10
print(­count) #will print numbers 0 - 9
count = count + 1 # must increase count

Decision Making­/Co­ndi­tional Statem­ents:
if 3 < 2: #if statement must compare two Booleans
print ('3 is less than 2')
elif 4 < 2: #can have 0 or more elif statements
print ('4 is less than 2')
elif 5 < 2:
print ('5 is less than 2')
else: #can have 0 or 1 else statement at the end
print ('none of the above are True')

Info

Vocabu­lary:
syntax, variable, Boolean, string, integer, float,
list, comment, character, condit­ional, modulo,
if/eli­f/else, loop, range, parameter, argument,
function call,

Data Types:
String - a list of characters e.g. "­abc­123­$%^­", or
empty string "­"
Integer - whole numbers, and negative numbers e.g. -5,
0, 2, 99
Floating Point - decimal numbers e.g. 1.5, 2.0, -2.99
Boolean - True or False

User input:
user_input = input(­"­Enter a value: ")

Converting between different data types:
word = str(3) #converts 3 to a string "­3"
num = int("3.5­") #converts "­3.5­" to an integer 3
num = float(­"­3") #converts "­3" to a float 3.0

Printing values:
print(­"­hel­lo", "­the­re") #displays hello there
print(­"­hel­lo" + "­the­re") #displays hellothere

Combining Strings (Conca­ten­ation)
"­hi" + "­the­re" == "­hit­her­e"
"­hi" * 5 == "­hih­ihi­hih­i"

Comments
# hashtag – everything after # is a comment not code
"­"­"
Double quote - Multi-line comment, everything in
between three double quotes is a comments
"­"­"
''' Single quote - Multi-line comment, everything in
between three single quotes is a comments '''

stop the loop

            mylist =[ ] 
          while True:
value = input("Enter value: ")
     if value == "*"
           break
        else:
     mylist.append(value)

print (mylist)
contin­uously ask the user for input if the user types star,stop the loop and print the list

create mylist: dont know what inside

for number in mylist:
   print (number)
Create a program which prints every element from a list called mylist[ ] : you do not know what is inside the list

pattern based on user input

1=    !
2=    !!
      !!
3=   !!!
     !!!
     !!!

determine whther user input is even or odd

number= int(input("Enter number: "))
   if number%2 ==0:
print (number, "is even num")
else:
print (number, "is odd num")

Python­5-M­ethods

#lists

shoppinglist = ['phone', 'battery', 'charger']

for item in shoppinglist:
    print (item)
for number in range (1, 10):
    print (number)
for number in range(5):
    print (number)
    
##################################################

#lists

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

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

Python­2-M­ethods

#write a program that converts a number to binary

#get a number from the user

user_number = int(input("Enter a number to convert to binary: "))

#while loop

#

while (user_number >0): #the number is greater than 0)
    remainder = 
    binary_string =
    binary_string = 

#after the loop print the binary string
print ("Binary string is", binary_string)

#expected output - 5 =101
#expected output - 3 =11
#expected output - 2 =10

Python­3-M­ethods

number= int(input("What's your number?")
            
while(number>=1):
            
            print(number)
            number=number-1
            
convert= int(input("What do you want to convert to?")
 

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.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter