Show Menu
Cheatography

milly Cheat Sheet by

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!

Condit­ionals

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

boolean

print (True)
print (2<3)
print (2 != 2)

maxvalue

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

print(max2(8,99))
print(max2(5,6))

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

print(max3(1,2,3))
print(max3(4,5,6))

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

    for item in mylist:
        if item > maxvalue:
            maxvalue = item
    
    return maxvalue

mylist = [1,5,9,10,13]
print(maxlist(mylist))

palindrome

reverse = ""
letter_num = 0

word = input('type in a word: ')

while letter_num < len(word):
    reverse = word[letter_num] + reverse
    letter_num = letter_num + 1

if reverse == word:
    print ("it is palindrome")
else:
    print ("it is not palindrome")

Addition

string + string
combine together
string + number
crash!
number + number
addition (math)

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
print (float(1)) #1.0 anything to a float

Modulo­/Re­member %
print (4%2) #0
print (30%7) #2
 

import random

import random

intlist = [1,2,3,4,5]
random_int = random.choice(intlist)
print (random_int)

fplist = [5.0,5.1,5.2,5.3,5.4,5.5]
random_fp = random.choice(fplist) 
print(random_fp)

strlist = ['dog','cat','bird','fish','fox']
random_str = random.choice(strlist)
print(random_str)

mylist = ['milly','earn','pim',1.1, 1.2]
random_item = random.choice(mylist)
print(random_item)

myvar1 = 1
myvar2 = 2
myvar3 = 3
varlist = myvar1, myvar2, myvar3
random_var = random.choice(varlist)
print (varlist, random_var)

printF­ibo­nac­cib­etw­een­0-5­0using loop

0,1,1,2,3,5,8,13,...(เค้าให้เลขมา)
num1 = 0
num2 = 1
fibonacci = num1 + num2
myoutput = "0,1"
while fibonacci: < 50:
    myoutput = myoutput + "," + str(fibonacci)
    num1 = num2
    num2 = fibonacci:
    fibonacci: = num1 + num2
print(myoutput)

recieve number and know that number is -,+,0

num = int(input("Enter a number"))
if num > 0:
    print(num, "is positive")
elif num < 0:
    print(num, "is negative")
else:
    print(num, "is zero")

recieve number that divide by 3

num = int(input("Enter your number:"))
remainder = num % 3
if remainder == 0:
    print(num, "is divisible by 3")
else:
    print(num, "is not divisible by 3")

convert number to integer and multiply product

num =int(input("Enter the number:"))
print(num * 5)

Countdown Code

user_n­umber = input(­"­Please enter a 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)

Naming Conven­tions

Rules for naming variables:
-letters
-numbers
-under­scores (_)
-can start with letters or unders­cores ONLY
-NO SPACES

Valid names:
- _mystr
- my3
Hello_­there

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

Vocabulary

variable
something that can change
string
a list of characters
intege­r/n­umber
pos/neg natural numbers and zero
floating point
decimal number
lenght
the lenght of the string
Modulo
Finds the remainder
Boolean
True/False
Syntax
Gramma­r/S­tru­cture of the language
 

This prints the true or false value using boolean

print(­True)
print (2<3)
print (2 != 2)

def area of circle

def areaOf­Cir­cle(r):
if r <= 0:
return "­Error: invalid radius­"
pi = 3.1415
area = pi r *2

return area
user_r­adius = float(­inp­ut(­"­Enter the radius: "))
print('The area of the circle is', areaOf­Cir­cle­(us­er_­rad­ius))

Spelling a string out in reverse ode

word = input(­"Type in an word; ")
reverse = " "
for letter in word:
reverse = letter + reverse
print ("Re­verse: ", reverse)

triangle and prism area

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', areaOfTrinagle(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_lengtth))

Functions

print0
displays inform­ation on the screen
input0
receives info from the user
int0
converts the value into an integer
str0
converts the value to a string
float0
converts the value to a floating point
len0
the lenght of the string
#
One line comment not include in code
'''
Multi-line comment

use a for loop to print....

0,01,012,0123,01234(เค้าจะให้เลขที่ต้องใส่มา)
mystring = ""
fpr num in range(5):
    print (mystring)

Symbols

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

about mystring fill in the blank

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

convert the integer and print product

user_number = int(input("Enter a number"))
print(user_number*5)

print even number using loop

user_number = -100
while user_number< -1:
    print(user_number)
    user_number = user_number +2
 

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.