Vocabulary
variable |
something that can change |
string |
a list of characters |
Function
print() |
show information that you want on the screen |
int() |
Change number to be number integer |
float() |
Change number to be decimal number |
input() |
Gain information 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/counting number |
Float number |
The number in decimal |
Syntax |
Grammar/Structure of language |
Modulo |
Find the remainder |
Boolean |
True/False |
Example
Print (2) - integer
Print (2.5) - floating point
Print ("Hello") - 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) |
Multiplication 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[index] + (reverse)
index = int(index) + 1
print ("Reverse:", reverse)
|
Convert to binary
user_number = ' '
while user_number != '0'
user_number = input ("Enter a number to convert to binary")
number = int(user_number)
binary_string = ' '
while (number > 0):
remainder = number%2
binary_string = str(remainder) + binary_string
number = number//2
print ("Binary string is", binary_string)
|
Countdown Machine
user_number = input("What number do you want to count down? ")
number = int(user_number)
countdown_string = ' '
while number > 0:
countdown_number = countdown_string + str(number) + " "
number = number - 1
#print(number)
print (countdown_string)
|
Naming Convention
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 |
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) |
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)
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment