Show Menu
Cheatography

Vocabulary

variable
something that can change
string
a list of characters

Function

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

Vocabulary

Variable
Hold a value and can be change
String
A list of character such as number, letter and symbols
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

Example

Print (2) - integer
Print (2.5) - floating point
Print ("He­llo­") - string
Print (mystr) - variable
Print (mystr­,"Hi­"­,2,1.0) -- commas

mystr = "­Hi"
mystr ← name
"­Hi" ← value can change

print (int(1.5)) → 1
print (int("2­")) → 2

Create Function

def calc(num1, num2, operation):
    # use if/elif/else to check what operation to do
    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)
    #call the correct function and return the answer

def sum(a, b):
    return a + b
    # calculate the sum of a and b

    # return the answer

def product(a, b):
    return a *b
    # calculate the product of a and b

    # return the answer

def diff(a, b):
    a - b
    #calculate the difference between a and b

    # return the answer

def div(a, b):
    if b != 0:
        return a /b
    else:
        print("Error")
    #calculate the division of a and b

    # return the answer

print(calc(10, 0, "div")) # division by zero

print(cal(1,2,"sum")) #output should be 3
print(calc (4, 2, "diff")) # output should be 2
print(calc (9, 3, "div" )) #output should be 3
print(calc (2, 12, "product")) #output should be 24
 

Math

==
equal to
!=
no equal to
<
less than
>
more than
<=
less than or equal to
>=
more than or equal to
%
Modulo, Find the remainder 33 % 10 == 3
// divide with answer as an integer. E.g. 5//2 == 2
/ divide with answer as a float. E.g. 5/2 == 2.5
True or anything is always True False and anything is always False

Addition

string + string
Combine together
string + number
CRASH!
number + number
Addition (Math)

Multip­lic­ation and Exponents

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

Define 1

# write definitions for the following words
# use a multi-line string to print them to the screen

def printDefinitions(word):   # define the function named printDefinitions

    if word == "variable":
        # variable

        print ("""

        a variable is reserved memory locations to store values.

        """)

    elif word == "function":
        #function
        print ("""

        a function is block of organized

        """)
    elif word == "function call":
        #function call
        print ("""

        a function call is function that already have code, and use it.

        """)
    elif word == "parameter":
        #parameter
        print ("""

        a parameter is something that put in function to define variable.

        """)
    elif word == "argument":
        #argument
        print ("""

        a argument is parameter

        """)
    elif word == "string":
        #string
        print ("""

        a string is characters in quotes

        """)

    else:
        print ("Unknown word")
    
while True:
    
    user_input = input("Enter a word to define: ")

    printDefinitions(user_input) # function call
 

Reverse Word

while True:
word = input(­"­Please enter a word")
index = 0
reverse = ' '
 
while int(index) < len(word):
        reverse = word[i­ndex] + (reverse)
        index = int(index) + 1
 
print ("Re­ver­se:­", reverse)

Convert to binary

user_n­umber = ' '
 
while user_n­umber != '0' 
user_n­umber = input ("Enter a number to convert to binary­")
number = int(us­er_­number)
binary­_string = ' '
 
while (number > 0):
     remainder = number%2
     binary­_string = str(re­mai­nder) + binary­_string
     number = number//2
 
print ("Binary string is", binary­_st­ring)

Countdown Machine

user_n­umber = input(­"What number do you want to count down? ")
number = int(us­er_­number)
countd­own­_string = ' '
 
while number > 0:
        countd­own­_number = countd­own­_string + str(nu­mber) + " "
        number = number - 1
#print­(nu­mber)
 
print (count­dow­n_s­tring)

Naming Convention

Rule for giving name
- letter
- numbers
- unders­core_

Valid name
- _myStr
- my3
- Hello_­there

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

Python

import random

intlist = [1,2,3­,4,5]
random_int = random.ch­oic­e(i­ntlist)
print (intlist, random­_int)


fplist = [1.5,2.5,­3.5­,4.5­,5.5]
random_fp = random.ch­oic­e(f­plist)
print (fplist, random_fp)


strlist = ['1', '2', '3', '4', '5']
random_str = random.ch­oic­e(s­trlist)
print (strlist, random­_str)


mylist = [1, 2, 3, 4, 5, 1.5, 2.5, 3.5, 4.5, 5.5, '1', '2', '3', '4', '5']
random­_item = random.ch­oic­e(m­ylist)
print (mylist, random­_item)


myvar1 = 1
myvar2 = 2
myvar3 = 3
varlist = [myvar1, myvar2, myvar3]
random_var = random.ch­oic­e(v­arlist)
print (varlist, random­_var)

radius

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

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.