Show Menu
Cheatography

python cheat sheet Cheat Sheet by

volcab­ulary

variable
something that can change
string
a list of characters
integer number
whole number­/co­unting number
float number
The number in decimal
syntax
gramma­r/s­tru­cture of language
modulo
find the remainder
boolean
true/false
argument
A value passed to a function (or method) when calling the function.
parameter
A named entity in a function definition that specifies an argument that the function can accept.

Function

print()
displays inform­ation on the screen
input()
receive info from the user
len()
show the number of word in the string
int()
convert string or decimal number into integer number
float()
convert string or integer number into decimal number
str()
convert value to word(s)
#
comment, no effect
range()
the list of number between two numbers
def
define function
while
create loop with some condition.
for
create loop with no condition

0 01 0123 01234

mystring = ""
for items in range(5):
    mystring = mystring + str(items)
    print(mystring)

#or

mystring = ""
index = 0
while index < 5:
    mystring = mystring + str(index)
    print (mystring)
    index = index + 1
 

Math

==
equal to
!=
no equal to
<
less than
>
more than
<=
less than or equal to
>=
more than or equal to
%
Modulo, find the remainder
/
divide with answer as a float. e.g. 5/2=2.5
//
divide with answer as an integer. e.g. 5//2=2
**
exponent

Naming Convention

Invalid name
-3my="h­i"
--cannot start with number
-first name="h­i"
-first­-name

Basic Calculator

def calc(num1, num2, operation):
    if operation=="sum":
       return sum(num1, num2)
    elif operation=="product":
        return product(num1, num2)
    elif operation=="diff":
        return diff(num1, num2)
    elif operation=="div":
        return div(num1, num2)
    else  :
        print ("No operation")
    
def sum(a, b):
    myvar1 = a+b
    return myvar1

def product(a, b):
    myvar2 = a*b
    return myvar2

def diff(a, b):
    myvar3 = a-b
    return myvar3

def div(a, b):
  if b!=0 :
      return (a//b)
  else:
      print ("Error")

print (calc(10,0,"div"))
print (calc(1,2,"sum"))
print (calc(4,2,"diff"))
print (calc(9, 3,"div"))
print (calc(2, 12,"product"))

Fibonacci

index = 0
index2 = 0
Fibo = ""
while index < 50:
    if index == 0:
        Fibo = Fibo + str(index)
        index = index + 1
    elif index > 0:
        Fibo = Fibo + ", " + str(index)
        index = index + index2
        index2 = index-index2 
print (Fibo)
 

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)

even and odd

even = 0
odd = 0
while True:
    mynum = int(input("Enter the positive number: "))
    if mynum > 0:
        if mynum%2 == 0:
            even = even + 1
        elif mynum%2 != 0:
            odd = odd+1
    elif mynum < 0:
        break
print (even, (" of them are even."))
print (odd, (" of them are odd."))

Guessing Game

import random 

mylist = ['happy', 'love' , 'flower' , 'golden' , 'valentine', 'rainbow']
score = 0
chance = 5
print (mylist)

random_item = random.choice(mylist)

while chance > 0 :
    user_guess = input("Guess a word: ")
    if user_guess == random_item:
        print ("That's correct!")
        score = score  + 100
        print ("Score: ",score)
        random_item = random.choice(mylist)
    else:
        chance = chance - 1
        print ("Chance Remaining: ",chance)
        if user_guess in mylist:
            print ("Sorry, wrong choices")
        else:
            print ("Sorry, that is not even in the list !")
print ("Final Score: ",score)
print ("Game Over!The word was",random_item)

While loop list

whileList = ['Guy','Pop','Pat','Kim','Cliff','Anon']
index = 0
while index < len(whileList):
    print (whileList[index])
    index = index + 1
 

Max value in list

#Example 1
def max2(num1, num2):
    maxvalue = num1

    if num2 > maxvalue:
        maxvalue = num2
    return maxvalue
print (max2(3,5))


#Example 2
def max3(num1, num2, num3):
    maxvalue = num1

    if num2 > num3 > maxvalue:
        maxvalue = num2

    elif num3 > num2> maxvalue:
        maxvalue = num3
    return maxvalue
print (max3(3,5,9))


#Example 3
def maxlist(list):
    maxvalue = list[0]
    for num in list:
        if maxvalue < num:
            maxvalue = num
    return maxvalue
print (maxlist(range(0,56)))

area of circle calcul­ation

def areaOfCircle(r):
    if r <= 0:
        return "Error"
    pi = 3.1415
    area = pir*2
    return area
user_radius = float(input("Enter the radius: "))
radius = float(user_radius)

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

palindrome

def isPalindrome(word):
    index = 0
    while index < 1/2*len(word):
          if word[index] == word[len(word)-1-index] :
             index = index+1
          else :
             return False
    return True

while True:
    word = input("Enter your word: ")
    if word == str("quit"):
        break
    else:
       if isPalindrome(word) == True :
             palindrome = (word + str(" is a palindrome."))
       else :
             palindrome = (word + str(" is not a palindrome."))
           
    

    print(palindrome)
 

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.