Show Menu
Cheatography

Math of Symbols

==
Equal to
!=
No equal to
<
Less than
>
More than
<=
Less than or equal to
>=
More than or equal to
%
Modulo, find the remainder
+
Add
-
Subtract
*
Multip­­li­c­ation
**
Exponent
/
Divide and quotient is float
//
Divide and quotient is integer

Text

single quoted
'example'
double quoted
"­exa­mpl­e"

Functions

print()
display inform­ation on the screen
input()
display inform­ation from the user
int()
convert a value to an integer
len()
The length of the string
float()
Change number to be decimal number
str()
converts the value to a string
#
Comment, no effect

What's is your name? code

mystring = "hello"
print (mystring)
firstname = input( "what is your first name?")
lastname = input( "what is your last name?")
fullname = firstname + " " + lastname
print (fullname)

letternumber = int(input( " what is letter number? " ))
if letternumber >len(fullname):
    print ( " invalid letter number, try again! " )
else:

    letter = ( fullname[letternumber] )
    print (letter)

    numberletter = int(input( "how many times to print letter " ))

    if numberletter >100:
       print ( " too many letters to print! " )
    else:
        print (letter * numberletter )
hello
what is your first name?
what is your last name?

what is letter number?

how many times to print letter

List Random

import random
intlist = [1,10,100]
random_int = random.choice(intlist)
print(random_int,intlist)

fplist = [2,20,200]
ramdom_fp =  random.choice(fplist)
print(ramdom_fp,fplist)

strlist = ('Pin','Anpan','Bella')
ramdom_str =  random.choice(strlist)
print(ramdom_str,strlist)

mylist = [1,10,100,2,20,200,'Pin','Anpan','Bella']
ramdom_item =  random.choice(mylist)
print(ramdom_item,mylist)

myvar1 = 1
myvar2 = 2
myvar3 = 3
varlist = [myvar1,myvar2,myvar3]
random_var =  random.choice(varlist)
print(random_var,varlist)
10 [1, 10, 100]
200 [2, 20, 200]
Bella ('Pin', 'Anpan', 'Bella')
2 [1, 10, 100, 2, 20, 200, 'Pin', 'Anpan', 'Bella']
3 [1, 2, 3]

bacon()

def bacon():
    print("hello it's bacon")
    print("line 2")
    print("line 3")
    print("line 4")
    return
bacon()
bacon()
bacon()
hello it's bacon
line 2
line 3
line 4
hello it's bacon
line 2
line 3
line 4
hello it's bacon
line 2
line 3
line 4

myprintnew (text, decora­tion)

def myprintnew (text, decoration):
    print(decoration + str(text) + decoration)
    return
myprintnew(1, "+++")
myprintnew('hello','-=-=-=-=-=-=-=-=')
myprintnew(1, "@@@@@@@")
+++1+++
-=-=-=­-=-­=-=­-=-­=he­llo­-=-­=-=­-=-­=-=-=-=
@@@@@@­@1@­@@@@@@

area of a triangle

def areaofTriangle(b, h):
    area = 0.5  b  h
    return area
user_base = float(input('Enter the base of the triangle:'))
user_height = float(input('Enter the height of the triangle:'))
print('The area of the triangle is',areaofTriangle(user_base, user_height))

def volumeofPrism(b,h,l):
    volume = areaofTriangle(b, h) *l
    return volume
user_lenght = float(input('Enter the length of the prism: '))
print('The volume of the prism is',volumeofPrism(user_base, user_height, user_lenght) )
Enter the base of the triang­le:­11111
Enter the height of the triang­le:2222
The area of the triangle is 12344321.0
Enter the length of the prism: 3333
The volume of the prism is 411436­21893.0
 

Vocabulary

Variable
Hold a value and can be changed
String
A list of characters such as number, letters, symbols
Integer number
Whole number/ counting number
Float number
The number in decimal
Syntax
Grammar / Structure of language
Modulo
Find the remainder
Boolean
True / False

Naming Conven­­tions

Rules for naming variab­­les:
- letters
- numbers
- unders­­cores (_)
- can start with lett­ers or unde­r­sc­ores ONLY
- NO SPACES

Valid names:
- _mystr
- my3
Hello_­­there

Invalid names:
- 3my= "­­hi­" -- cannot start with number
- first name = "­­hi­" -- no spaces allowed
- first­­-­name -- dashes are not accepted

Example

Print (2) – integer
Print (2.5) – floating point
Print (“Hello”) – string
Print (mystr) – variable
Print (mystr­­,”­H­i­”,­­2,1.0) -- commas

mystr = “Hi”
mystr ← name
“Hi” ← value can change

print (int(1.5)) → 1
print (int(“2”)) → 2
print (float(1)) → 1.0 anything to a float

Modulo­­/R­e­m­ainder %
print (4%2) → 0
print (30%7) → 2

A radius of a circle code

#Ask the user for a radius of a circle
user_r­adius =input­("What is the radius of the circle­?")

#Convert the given radius to a floating point
radius = float(­use­r_r­adius)
#make a variable called pi
pi = 3.1415

#Calculate the area of the circle using exponents
area = piradius*2

#display the area of the circle to the user
print(­"The area of the circle is" ,area)
What is the radius of the circle?123
The area of the circle is 47527.7­53­500­000006

Reverse Code

word =input­("Please enter your name: ")

index = 0

reverse =''

while index < len(word):
reverse = word[i­ndex]+ reverse
index = index + 1


print ("Re­verse: "­,re­verse)
Please enter your name: Timmy
Reverse: ymmiT

myprin­t(text)

def myprint(text):
    print("" + str(text) + "")
    return
myprint(1)
myprint("hello")
myprint(2.5)
1
hello
2.5

areaOf­Cir­cle(r)

def areaOfCircle(r):
    if r <= 0:
        return "Error: invalid radius"
    pi = 3.1415
    area = pi  r * 2
    return area
user_radius = float(input("Enter the radius:"))
print ('The area of the circle is', areaOfCircle(user_radius))
Enter the radius:300
The area of the circle is 282735.0
Enter the radius:0
The area of the circle is Error: invalid radius

_var1

_var1 = 1
_var1 = 3
_var1 + 100
print(_var1)
3

maxlist

def maxlist(list):
    maxvalue = list[0]
    for item in list:
        if item > maxvalue:
            maxvalue = mylist
    return maxlist
mylist = [21365741,2135416,2,54131,1.1515]
print(maxlist(mylist))

maxvalue

def max2(num1,num2):
    maxvalue = num1
    if num2 > maxvalue:
        maxvalue = num2    
    return maxvalue
print('The largest number is',max2(2,3))
print('The largest number is',max2(12222,10))

def max3(num1,num2,num3):
    maxvalue = num1
    if num2 > maxvalue:
        maxvalue = num2
    if num3 > maxvalue:
        maxvalue = num3
            
    return maxvalue
print('The largest number is',max3(1,5,10))
print('The largest number is',max3(12222,5,10))
print('The largest number is',max3(12222,164.3415645,121348561240))
The largest number is 3
The largest number is 12222
The largest number is 10
The largest number is 12222
The largest number is 121348­561240
 

Multip­lic­ation and Exponents

string * number
combine that string multiple times
string * string
crash
number * number
math - multiply
string ** string
crash
number ** number
math - multiply
string ** number
crash

Addition

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

Condit­­ionals

If..... :then..... else.......
If the statement is true then do command under then else do command under else
while......
While this is true loop the command under the condit­­iona
While True
loops forever
for each item in name of list
For every item in the list repeat the command under the loop that many times. (a string is a list too)

Big or small code

mystr = "hello THERE"
print (mystr.upper())
print (mystr.lower())
print (mystr.capitalize())
print (mystr.title())
HELLO THERE
hello there
Hello there
Hello There

Please enter a number Code

user_number = input("Please enter a number: ")

number = int(user_number)

countdown_string = ''

while number > 0:
        countdown_string = countdown_string+ str(number)
    
        number = number-1

print (countdown_string)

Sort word per line

mystr = "­­­H­e­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

Shoping List code

shoppi­nglist = ['shoes', 'bags', 'shirts', 'pants']

index = 0

while index < len(sh­opp­ing­list):
print (shopp­ing­lis­t[i­ndex])
index = index + 1

for item in shoppi­nglist:
print (item)
shoes
bags
shirts
pants
shoes
bags
shirts
pants

printD­efi­nitions




def printDefinitions(word):
    if word =="variable":
        print ('....')
    elif word =="function":
        print ('....')
    elif word =="parameter":
        print ('....')
    elif word =="argument":
        print ('....')
    elif word =="function call":
        print ('....')
    elif word=="string":
        print ('....')
    else:
        print("unknown word")
    return
while True:
    user_input = input("Enter word: ")
    printDefinitions(user_input)
Enter word:

area of a triangle

def areaofTriangle(b, h):
    area = 0.5  b  h
    return area
user_base = float(input('Enter the base of the triangle:'))
user_height = float(input('Enter the height of the triangle:'))
print('The area of the triangle is',areaofTriangle(user_base, user_height))

def volumeofPrism(b,h,l):
    volume = areaofTriangle(b, h) *l
    return volume
user_lenght = float(input('Enter the length of the prism: '))
print('The volume of the prism is',volumeofPrism(user_base, user_height, user_lenght) )
Enter the base of the triang­le:­11111
Enter the height of the triang­le:2222
The area of the triangle is 12344321.0
Enter the length of the prism: 3333
The volume of the prism is 411436­21893.0

double­It(­number)

def doubleIt(number):
    return number*2
print (doubleIt(3))
print (doubleIt(doubleIt(4)))

myvar = 12
myvar= doubleIt(myvar)
myvar= doubleIt(myvar)
print(myvar)
6
16
48
 

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.