|
variable
|
something that can change
|
|
string
|
a list of characters
|
|
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
|
|
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
|
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
|
==
|
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
|
|
|
string + string
|
Combine together
|
|
string + number
|
CRASH!
|
|
number + number
|
Addition (Math)
|
|
string * number
|
Combine that string
|
|
string* string
|
CRASH!
|
|
number * number
|
Multiply (Math)
|
|
string ** string
|
CRASH!
|
|
number ** number
|
Exponent (Math)
|
|
string ** number
|
CRASH!
|
# 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
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)
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)
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)
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)
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.