Show Menu
Cheatography

Vocabulary

Modulo(%)
Finding the remainder
Syntax
Grammar/ Structure of language
/
divide­(float)
//
divide­(in­teger)

Comparing Values

True or anything
True
False and anything
False

Multip­­­­­l­i­­c­­­ation and Exponents

string* string
Fail!!
string ** string
Fail!!
string ** number
Fail!!

Random

import random
int/fp­/st­rlist = [1,2,3­,4,­5,6­,7,­8,9,0]
random­_in­t/f­p/str = random.ch­oic­e(i­nt/­fp/­str­list)
print(­int­/fp­/st­rli­st,­ran­dom­_in­t/f­p/str)

#fp = float ,str = string
fplist = [4.6,3.2,­7.7­,6.2]
strlist = ['uik'­,'l­ok'­,'p­ki'­,'roo']

mylist = [589,5­6.3­,'s­uay']
random­_mylist = random.ch­oic­e(m­ylist)
print(­myl­ist­,ra­ndo­m_m­ylist)

Palindrome function

user_input = input("write down string")
print(input)

letter_num = 0
reverse = ""
while letter_num < len(user_input):
    reverse = user_input[letter_num] + reverse 
    letter_num = letter_num + 1

print ("Reverse: ",reverse)

if reverse == user_input:
    print("This is palindrome")
else:
    print("This is not a palindrome")

Guessing Game

import random
mylist = ['lion', 'cheetah', 'panther', 'cougar', 'leopard']
random_item = random.choice(mylist)
Chances = 5
Score = 0

while Chances > 0:
    print("Words:['lion', 'cheetah', 'panther', 'cougar', 'leopard']")
    
    
    user_guess = input("Guess a word: ")
    if user_guess == random_item:
            print("That's correct!")
            Score = Score+100
            print("Chances remaining",'',Chances)
            random_item = random.choice(mylist)
            print("Score is",'',Score)
    else:
      
        if user_guess in mylist:
                print("Sorry, wrong choice!")
                Chances = Chances - 1
                print("Chances remaining",'',Chances)
                print("Score is",'',Score)
        else:
                print("Sorry, that is not even in the list!")
                Chances = Chances - 1
                print("Chances remaining",'',Chances)
                print("Score is",'', Score)

if Chances == 0:
    print("Gameover",'',"The word is",'',random_item)
    print("Final score is",'',Score)
 

Printing Value

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"

List

mylist.ap­pend(5) #add at the end of the list

Range

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

in

if user_guess in mylist:
        print("Sorry, tht's wrong")
    else:
        print("Sorry It is not in choice")

Comment

# comment

"­"­"
Double quote
for 2 line comment
"­"­"
''' Single quote - Multi-line comment, '''

Find the area of prism

def volumeofprism(b,h,l):
    volume = areaofTriangle(b,h) * l
    return volume
user_length = float(input('Enter the length of the prism: '))
print ("The volume of the prism is", volumeofprism(user_base,user_height,user_length))

Print even number

num = 2
while 1 < num and num <101 :
    print(num)
    num = num + 2

Function Largest Value

def max3 (num1,num2,num3):
    if num1>num2 and num1>num3:
        largestvalue = num1
    elif num2>num3 and num2>num1:
        largestvalue = num2
    else:
        largestvalue = num3 
    return largestvalue

print (max3(9,100,25))
print (max3(69,85,1))
print (max3(75,9,33))



def maxlist (list):
    largestvalue = list [0]
    for item in list:
       if item > largestvalue:
           largestvalue = item
    return largestvalue
mylist = [1,2,3,4,103,100,89,57]
print (maxlist(mylist))

Palind­rome2

def isPalindrome(word):
    letter_num = 0
    reverse = ""

    while letter_num < len(user_word):
        reverse = user_word[letter_num] + reverse 
        letter_num = letter_num + 1
        
    
    if  word == reverse:
        return True
    else:
        return False

while True:
    user_word = input("Enter the word")
    word = len(user_word)
    print(word)
    if user_word == "quit":
        break
    if isPalindrome(user_word):
        print(user_word, "is a palindrome")
    else:
        print(user_word, "is not a palindrome")
 

Capital letter

name = "tim GIRARD­­­"

print (name.u­­­­p­p­­er()) → TIM GIRARD
print (name.l­­­­o­w­­er()) → tim girard
print (name.c­­­­a­p­­i­­ta­­­li­ze()) → Tim girard
print (name.t­­­­i­t­­le()) → Tim Girard

Naming Conven­­tions

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

Countdown Number

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

Reverse Number

word = input("enter the word")
letter_num = 0
reverse = ""
while letter_num < len(word):
    reverse = word[letter_num] + reverse 
    letter_num  = letter_num + 1

print ("Reverse: ",reverse)

Convert to Binary String

user_number = input ("Please enter the number")
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)

Sort word per line

mystr = "­­He­l­l­o"

letter_num = 0

while letter_num < len(my­­str):
     print (mystr­­[l­e­t­te­­r_num])
     letter_num = letter_num + 1

H
e
l
l
o

Define: function area of circle

def areaofcircle (r):
    if r <= 0:
        return "Error: Invalid radius"

    pi = 3.1415
    area = pi*r**2
    return area
user_radius = input ('Enter the radius')
r = float(user_radius)
print("The area of circle is",areaofcircle(r))

Definition

def printdefinitions(word):
    if word == "Variable":
        print ('lo')

    elif word == "Function":
        print ('fun')

    elif word == "Parameter" or word == "Argument": 
        print ('para')

    elif word == "Function call":
        print ('call')

    elif word == "String": 
        print ("stri")

    else:
        print ("Unknown Word") 
    return

while True: 
    user_input = input ("Enter the word:")
    printdefinitions(user_input)
 

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.