Show Menu
Cheatography

Vocabulary

Variable
Holds a value and can be change
String
a list of characters such as numbers, letters, symbols
Integer number
Whole number­/co­unting number
Float number
Number in decimal
Syntax
Gramma­r/S­tru­cture of language
Boolean
True/False
length
the length of the string

Function

Print()
Show inform­ation that you want on screen
Int()
Change number to be number integer
input()
receives info from the user
str()
converts the value to a string
float()
converts the value to a floating point
len()
The length of the string
#
comment,no effect
'''
Multi-line comment

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

string * number
string­­st­i­n­g...(­­number)
string* string
Fail!!
number * number
Multiply
string ** string
Fail!!
number ** number
Exponent
string ** number
Fail!!

Convert to Binary String

 
user_n­umber = ' '

while user_name != ' '
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­nde­r)+­bin­ary­_string
number = number//2

print ("Binary string is", binary­_st­ring)

Simple Function

def printdefinitions(word):
    if word == ("variable"):
        print ("""A variable is the value that can change""")
    elif word == ("function"):
        print ("""A function is the blog of code that can be reused""")
    elif word == ("parameter"):
        print ("""A parameter is something given to the function""")
    elif word == ("agrument"):
        print ("""An agrument is something given to the function""")
    elif word == ("string"):
        print ("""A string is a lsit of characters""")
    elif word == ("function call"):
        print ("""A function call makes your function run""")
    else:
        print ("Unknown word")
    return
while True: #keep the loop go forever
    user_input = input("Enter word: ")
    
    printdefinitions(user_input)

Simple Function

def printdefinitions(word):
    if word == ("variable"):
        print ("""A variable is the value that can change""")
    elif word == ("function"):
        print ("""A function is the blog of code that can be reused""")
    elif word == ("parameter"):
        print ("""A parameter is something given to the function""")
    elif word == ("agrument"):
        print ("""An agrument is something given to the function""")
    elif word == ("string"):
        print ("""A string is a lsit of characters""")
    elif word == ("function call"):
        print ("""A function call makes your function run""")
    else:
        print ("Unknown word")
    return
while True: #keep the loop go forever
    user_input = input("Enter word: ")
    
    printdefinitions(user_input)

Enter word: hello
Unknown word
Enter word: function
A function is the blog of code that can be reused
Enter word: variable
A variable is the value that can change
Enter word:
area/v­olume of
 

Symbols

==
equal to
!=
not equal to
<
less than
<=
less than or equal to
>
greater than
>=
greater than or equal to
+
add
-
subtract
*
multiply
/
divide and quotient is float
//
divide and quotient is integer
**
exponent
%
modulo: the remainder

Addition

string + string
combine together
string + number
Fail
number + number
plus
number - number
minus

Sample code

mystr = "hellp THERE"

print (mystr.upper()) -all letters will become big HELP THREE
print (mystr.lower()) -all letters will become small help three
print (mystr.capitalize()) -First letter of first word will become big Help three
print (mystr.title())- first letter of each words will become big Help Three

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

Area of the circle

 
def areaOf­Cir­cle(r):
pi = 3.1415
area = pir*2
return area
user_r­adius = float (input­("Enter the radius: "))
print ('The area of the circle is',ar­eaO­fCi­rcl­e(u­ser­_ra­dius))

MaxValue

def max2(num1,num2):
    maxvalue = num1
    if num2 > maxvalue:
        maxvalue = num2
  
    return maxvalue


print (max2(4,5))
print (max2(33,5))


def max3(num1,num2,num3):
    
    maxvalue = num1

    if num2 > maxvalue:
        maxvalue = num2
    if num3 > maxvalue:
        maxvalue = num3
    
    return maxvalue
print (max3(1,2,3))

5
33
3

Maxlist

def maxlist(list):
    maxvalue = list[0]

    for item in list:
        if item > maxvalue:
            maxvalue = item

    return maxvalue
mylist = [1,2,3,4,55,66,777,0,1]
print(maxlist(mylist))
777
 

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

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

circle area

def areaofcircle(radius):
    if radius <= 0:
        return "Error: invalid raadius"

    pi = 3.1415
    area = pi (radius*2)
    return area

user_radius = float(input("Enter the radius: "))

print('The area of the circle is', areaofcircle(user_radius))

Enter the radius: 2
The area of the circle is 12.566

Enter the radius: 0
The area of the circle is Error: invalid raadius

Countdown Number

 
user_n­umber = input(­"­Please enter the number­")
number = int(us­er_­number)
countd­own­_string = "­"
while number­>0:
countd­own­_string = countd­own­_string + str(nu­mber)
number = number - 1
print (count­dow­n_s­tring)

Palindrome

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

    if reverse==word:
        return True
    else:
        return False
    
while True :

    user_input = input("Enter a word")

    if user_input == "quit":
        break
    

    isPal = isPalindrome(user_input)
    
    if isPal == True:
        print (user_input,'is parindorm')
    else:
        print (user_input,'is not parindorm')
        break
Enter a word113311
113311 is parindorm
Enter a word123
123 is not parindorm
Enter a wordquit

Short word per line

mystr = "Hello"

letter_num = 0

while letter_num < len(mystr):
         print (mystr[letter_num])
         letter_num = letter_num + 1


H
e
l
l
o

Basic Function

def myprint(text):
    print ("" + str(text) + "")

    return

myprint("opal")

hello it's bacon
opal

def myprintnew(text, decoration):
    print (decoration + str(text) + decoration)
    return
myprintnew("opal", "m")

hello it's bacon
opal
mopalm

def doubleit(number):
    return number * 2

print (doubleit(3))
print (doubleit(doubleit(4)))


hello it's bacon
opal
mopalm
6

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

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.