Show Menu
Cheatography

Vocabulary

variable
something that can change
string
a list of character

Addition

string + string
combines the strings together Ex: "­1" + "­2" = 12
string + number
crash
number + number
math (addition)

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 remaider
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
 
mystr (int(int)) → 1
print (int("2­")) → 2
print (float(1)) → 1.0 anything to a float

Python :p

def max2 (num1, num2):
    if num1 > num2:
        return (num1)
    elif num1 < num2:
        return (num2)

print (max2(2, 3))


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

    if maxvalue < num2:
        maxvalue = num2
    if maxvalue < num3:
        maxvalue = num3
    return maxvalue
print (max3 (8,4,3))


def maxlist (list):
    maxvalue =(list[0])
    for item in list:
         maxvalue < item
         maxvalue = item
    return maxvalue
print (maxlist(range(0,123)))

Naming Convention

Rule for giving name

♦ letter
♦ number
♦ underscore _

Valid name

♦ _myStr
♦ my3
♦ Hello_­there


Invalid name

♦ 3my+"hi­" -- cannot start with number
♦ frist name ="hi­"
♦ frist-name

Condit­ional Loop

num = 0 or count =0
while count < 10 # loop while count is less than 10
           print(count)
           count = count + 1

Loop

Given list called mylist

for item in mylist :
            print(item)

num = 0
while num < len(mylist) :
     print (mylist[num])
     num = num+1
 

Function

print()
displays inform­ation on the screen
input()
receives info from the user
int()
converts a value to a floating point
str()
converts a value to a string

Math

= =
equal to
!=
no equal to
<
less than
>
more than
<=
less than or equal to
>=
more than or equal to
%
Modulo, Find the remainder
**
กำลัง2
and
or
not
string * string
multiply
/ divide with answer as float
Ex: 5/2 == 2.5
// divide with answer as integer
Ex: 5//2 == 2

Multip­lic­ation & Exponent

String * number
Combine that string Ex: "­2"*3 = 222
string * string
Crash
number * number
Multiply (Math)
sting ** string
Crash
number **number
Exponent (Math) 2 ** 3 = 2 power 3
string ** number
Crash

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)
 
print ("Re­verse: ", reverse)

Function

def calc(num1, num2, operation):
    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)

def product(a, b):
    return a*b 

def diff(a, b):
    return a-b

def div(a, b):
    return a/b

print(calc(10, 0, "div"))

print(calc(1,2,"sum"))
print(calc (4, 2, "diff"))
print(calc (9, 3, "div"))
print(calc (2, 12, "product"))

Func call areaOf­Ellipse ellpise use (pie r 1 r 2)

function should given 2 parameter (radius1 and radius2) should return area

def areaOfEllipse (radius1, radius2):
       pi = 22/7  / 3.1415
       area = pi  radius1  radius2
       return area

while loop -100 to -1 ( even number)

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


num = 0
while num > -100:
         num = num -2
         print (num)
 

Python♠

import random

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


fplist = [1.5,2.5,3.5,4.5,5.5]
random_fp = random.choice(fplist)
print (fplist, random_fp)


strlist = ['1', '2', '3', '4', '5']
random_str = random.choice(strlist)
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.choice(mylist)
print (mylist, random_item)


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

Python

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


    if word  == "variable":
        # variable

        print ("""
        a variable is something that can be changed
        """)

    elif word == "function":
        #function
        print ("""
        a function is block of organized
        """)


    elif word == "function call":
        #function call
        print ("""
        a function is function that already that have code
        """)

 elif word == "parameter":
        #parameter
        print ("""
        a function is something that put in function to define variable.
        """)


    elif word == "argument":
        #argument
        print ("""
        a function is the sane as parameter
        """)


    elif word == "string":
        #string 
        print ("""
        a function is character in quotes
        """)

    else:
        print ("unknow word")


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

    printDefinitions(user_input) #function call

Write function called multip­lic­ation Table

enter number: 5                          5 = user_input      1-10 = num
5 * 1 = 5
<<<<<                        
5 * 10= 50

def multiplication():
                 user_input = int(input("Enter a number:"))
                 num = 1
                  while num <= 10:
                                      print(user_input,"",num,"=",user_inputnum)
                                      num = num+1

Repeat Receive (+)int ,user enter (-)int exit loop

evenCount = 0
oddCount = 0
while True:
    user_input = int(input("Enter num:")
    if user_input < 0:
         print("Total even:",evencount)
         print)"Total odd:",oddcount)
         break
     elif user_input > 0
          if user_input %2 ==0;         #even
                evencount = evencount + 1
          else:
                 oddcount = oddcount + 1

output 4 is positive 0 is zero -8 is negative

user_num = input ("Enter the number")
user_num = int(user_num)
If user_num == 0
      print (user_num,"iszero")
elif user_num < 0:
      print (user_num," is negative")
elif user_num > 0:
      print( user_num, "is positive")

Recieve num user determine number divisible by 3

9 is divisible by 3
7 is not

user_num = input ("Enter the number")
user_num = int(user_num)
  If : user_num% 3==0
      print(user_num,"is divisible by 3")
  else:
      print(user_num," isnot divisble by 3")

Receive input from user as float print half number

user_input = input("Enter the number: "))
user_input = float(user_input) 
print(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.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter