Vocabulary
Variable |
Something that can be change |
String |
A list of characters |
Integer Number |
Whole number/counting number |
Float Number |
The number in decimal |
Syntax |
Grammar |
Modulo |
Remainder |
Boolean |
True&False |
Function
print() |
display info. on the screen |
input() |
receives info from the user |
int() |
convert a value to an interger |
float() |
convert a value to a floating point |
str() |
convert a value to a string |
len() |
The length of the string |
# |
comment |
if then else |
if ... is True "then" will work or if it False then "else" will work |
While: True |
Forever Loop |
for |
Looping Forever |
Function largest number
def max2(num1,num2):
if num1 > num2:
maxvalue = num1
elif num2 > num1:
maxvalue = num2
return maxvalue
#function call
print(max2(92,3))
print(max2(4,5))
def max3(num1,num2,num3):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
elif num3 > maxvalue:
maxvalue = num3
return maxvalue
#function call
print(max3(92,5,6))
print(max3(8,9,7))
print(max3(99,40,49))
def maxlist(list):
maxvalue = list[0]
for num in list:
if maxvalue < num:
maxvalue = num
return maxvalue
mylist = 3,5476,134,8,451,87
print (maxlist(mylist))
|
Naming Convention
Rules for giving name |
-letter |
-numbers |
-underscores |
Valid Name |
- _myStr |
- my3 |
- Hello_there |
Invalid Name |
- 3mt = "hi" -- cannot start with number |
- first name = "hi" |
- first-name |
Volume and Triangle
def areaOfTriangle(base,height):
area=1/2 base height
return(area)
user_base = float(input('Enter the base of the triangle: '))
user_height = float(input('Enter the height of the triangle: '))
#function call
print('The area of the triangle is',areaOfTriangle(user_base,user_height))
def volumeOfPrism(base,height,prism_height):
volume = areaOfTriangle(base,height) * prism_height
return (volume)
user_prism_height = float(input('Enter the prism height:'))
print ('the volume of the prism is', volumeOfPrism(user_base, user_height, user_prism_height))
|
Palindrome
def isPalindrome(word):
if reverse_item == user_word:
return "true"
else:
return "false"
while True:
user_word = input('What is the word: ')
length = len(user_word)
if user_word == 'quit':
break
else:
print("Length of the word is: ",(length))
reverse = ""
for item in user_word:
reverse = item + reverse
reverse_item = reverse
s = 0
while s < length / 2 + 1 :
if reverse_item[s] == user_word[s]:
print(user_word, "is a palindrome")
break
s = s + 1
else:
print(user_word, "is not a palidrome")
break
|
|
|
Math
= = |
equal to |
!= |
not equal to |
< |
less than |
> |
greater than |
<= |
less than or equal to |
>= |
greater than or equal to |
% |
Modulo, Find the remainder |
Addition
String + String |
combine together |
string + number |
CRASH |
number + number |
math |
Multiplication and Exponent
string * number |
combine the string |
string * string |
CRASH |
number * number |
math |
string ** string |
CRASH |
number ** number |
Math |
string ** number |
CRASH |
Example
Print (2) -interger |
Print (2.4) - Floating Point |
Print (myStr) - variable |
Print ("Hello") - string |
Print (myStr,"Hi",2,1,0) -- commas |
myStr = "Hi" |
mystr - name |
"Hi" - value can change |
print (int(1.5)) - 1 |
print (float(1)) - 1.0 |
print (4%2) - 0 |
Randomizer
"""
Group member: Kim, Pop, and Earn
Group: 10-03
"""
import random
mylist = ('lion' , 'tiger' , 'dog' , 'bee' , 'cat')
print (mylist)
random_item = random.choice(mylist)
print (random_item)
chance = 7
score = 0
while chance > 0:
user_guess = input("Guess a word: ")
if user_guess == random_item:
score = score + 100
print ("That's Correct!")
print ('score=',(score))
random_item = random.choice(mylist)
else:
if user_guess in mylist:
chance = chance - 1
print ("Sorry, wrong choice")
print ("chance =", (chance))
print ("The word is: ",random_item)
random_item = random.choice(mylist)
print ("score",(score))
else:
chance = chance - 1
print ("Sorry, that is not even in the list")
print ("chance =", (chance))
random_item = random.choice(mylist)
print ("score",(score))
if chance == 0:
print ("GAME OVER!")
print (' Answer =', (random_item))
print ("Final Score =", (score))
|
Area
def areaOfCircle(r):
pi = 3.1415
area = 2pir
return area
user_radius = float(input("Enter the radius: "))
print("The area of the circle is", areaOfCircle(user_radius))
|
Calculating Program
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 sum(a, b):
return (a + b)
def product(a, b):
return (a * b)
def diff(a, b):
return (a - b)
def div(a, b):
return (a / b)
print (calc(1,2,"sum"))
print(calc (4,2, "diff"))
print (calc (9, 3, "div"))
print(calc (2, 12, "product"))
|
Palindrome2
'''
Kim Jaroensattayatham(1003)
'''
def isPalindrome(word):
index = 0
reverse = ""
while int(index) < len(word):
reverse = word[index] + reverse
index = int(index) + 1
if reverse == user_input:
return "true"
else:
return "false"
while True:
user_input = input("please enter the word")
if user_input == "quit":
break
print(len(user_input))
print(isPalindrome(user_input))
|
|
|
Reverse Word
while True:
word = input("enter your 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 countdown")
number = int (user_number)
countdown_string = ' '
while number > 0:
countdown_number = countdown_string + str(number) + " "
number = number - 1
#print(number)
print(countdown_string) |
IMPORT
import random
intlist = [1,2,3,4,5]
random_int = random.choice(intlist)
print(intlist, random_int)
fplist = [1.0,1.2,1.3]
random_fp = random.choice(fplist)
print(fplist, random_fp)
strlist = ['a','b','c','d','e']
random_str = random.choice(strlist)
print(strlist, random_str)
mylist = [1,1.0,'hello']
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)
|
Asking Name Code
firstname = input(" what is your firstname? ")
lastname = input(" what is your lastname?")
fullname = firstname + " " + lastname
print (fullname)
letternum = int(input(" what is the letter number? "))
if len(fullname) < letternum :
print("Invalid letter number, try again! ")
else :
print (fullname[letternum])
letterprint = int(input(" how many time to print letter "))
if letterprint > 100 :
print (" too many letters to print ")
else :
print (fullname[letternum] * letterprint) |
rdtfghjbkn
'''
theList = ('A' , 'B' , 'C' , 'D')
for item in theList:
print (item)
whileList = ('a' , 'b' , 'c')
index = 0
while index < len(whileList):
print(whileList[index])
index = index + 1
while True:
user_input = input("Enter your word")
print (len(user_input))
if user_input == "exit":
break
def theFunction():
while True:
user_input = input ("Enter the word:")
if user_input == "stop":
break
#call the function
theFunction()
def computeThis(a1,b2):
theproduct = a1 * b2
print(theproduct)
return
computeThis(1,2)
'''
def finalFunction(string):
print("*",string,"*")
return
finalFunction("Kim")
|
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment