|
variable
|
something that can change
|
|
string
|
a list of character
|
|
string + string
|
combines the strings together Ex: "1" + "2" = 12
|
|
string + number
|
crash
|
|
number + number
|
math (addition)
|
|
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 remaider
|
|
Boolean
|
True/False
|
|
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
|
|
mystr (int(int)) → 1
|
|
print (int("2")) → 2
|
|
print (float(1)) → 1.0 anything to a float
|
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)))
num = 0 or count =0
while count < 10 # loop while count is less than 10
print(count)
count = count + 1
Given list called mylist
for item in mylist :
print(item)
num = 0
while num < len(mylist) :
print (mylist[num])
num = num+1
|
print()
|
displays information on the screen
|
|
input()
|
receives info from the user
|
|
int()
|
converts a value to a floating point
|
|
str()
|
converts a value to a string
|
|
= =
|
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
|
|
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
|
while True:
word = input("Please enter a word")
index = 0
reverse = ' '
while int(index) < len(word)
reverse = word[index] + (reverse)
index = int(index)
print ("Reverse: ", reverse)
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"))
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
num = -100
while num < -1:
print (num)
num = num + 2
num = 0
while num > -100:
num = num -2
print (num)
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)
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
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_input*num)
num = num+1
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
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")
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")
user_input = input("Enter the number: "))
user_input = float(user_input)
print(user_input)
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.