Vocabulary
Variable |
Hold a value, can be change |
string |
A list of number/letter/symbols |
Float number |
Whole number/counting number |
integer number |
The number in decimal |
boolean |
True/False |
Modulo |
Fine the remainder |
syntax |
Grammar/structure of lauguage |
length |
the length of the string |
Function
print() |
Show information that you want on the screen |
input() |
Gain information from user |
Float() |
Change to the decimal number |
int() |
change to the number integer |
str() |
A list of number/letter/symbols |
len() |
The length of the string |
# |
To note, no effect |
"" |
Multi-line comment |
Conditionals
If..... :then..... else....... |
If the statement is true then do command under then else do command under else |
while...... |
While this is true loop the command under the conditional |
While True |
loops forever |
for each item in name of list |
For every item in the list repeat the command under the loop that many times. (a string is a list too) |
Naming Conventions
Rules for naming variable:
- letters
- numbers
- underscores (_)
- can start with letters or underscores ONLY
- NO SPACES
Valid names:
- _mystr
- my3
Hello_there
Invalid names:
- 3my= "hi" -- cannot start with number
- first name = "hi" -- no spaces allowed
- first-name -- dashes are not accepted |
Example8-Guessing game
import random
mylist = ['lion', 'cheetah', 'panther', 'cougar', 'leopard']
random_item = random.choice(mylist)
Chances = 5
Score = 0
while Chances > 0:
print("Words:['lion', 'cheetah', 'panther', 'cougar', 'leopard']")
user_guess = input("Guess a word: ")
if user_guess == random_item:
print("That's correct!")
Score = Score+100
print("Chances remaining",'',Chances)
random_item = random.choice(mylist)
print("Score is",'',Score)
else:
if user_guess in mylist:
print("Sorry, wrong choice!")
Chances = Chances - 1
print("Chances remaining",'',Chances)
print("Score is",'',Score)
else:
print("Sorry, that is not even in the list!")
Chances = Chances - 1
print("Chances remaining",'',Chances)
print("Score is",'', Score)
if Chances == 0:
print("Gameover",'',"The word is",'',random_item)
print("Final score is",'',Score)
|
Example13- def printDefinitions
def printDefinitions(word):
#variable
if word == "variable":
print ("""
A varible is value that can be change
""")
#function
elif word == "function":
print ("""
A function is block of quote can be reused
""")
#parameter
elif word == "parameter":
print ("""
A parameter is value that inside the blacket of the function
""")
#argument
elif word == "argument":
print ("""
A argument is value that inside the blacket of the function
""")
# function call
elif word == "function call":
print ("""
A function call is something that make the function run
""")
#string
elif word == "string":
print ("""
A string is list of character
""")
else:
print("unknown word")
return
user_input=input("Enter word")
printDefinitions(user_input)
|
Example17-The largest value
#write a function that returns the largest of two values
#name : max2
#agruments: num1, num2
# return: largest value
# write a functrion that returns the largest of three values
# name : max3
#agrument: num1, num2, num3
# return: largest value
def max2(num1,num2):
if num1 >= num2:
max_value = (num1)
if num2 > num1:
max_value = (num2)
return max_value
num1 = input('Enter the the first value')
num2 = input('Enter the the second value')
print (max2(num1,num2))
def max3(num1,num2,num3):
if num1 >= num2 and num1 >= num3:
max_value = (num1)
if num2 > num1 and num2 >= num3:
max_value = (num2)
if num3 >= num2 and num3 >= num1:
max_value = (num3)
return max_value
num3 = input('Enter the the third value')
print (max3(num1,num2,num3))
|
|
|
Symbols
== |
equal to |
!= |
not equal to |
< |
less than |
<= |
less than or equal to |
> |
greater than |
>= |
greater than or equal to |
+ |
add |
- |
subtract |
* |
multiply |
/ |
divide and quotient is float |
// |
divide and quotient is integer |
** |
exponent |
% |
modulo: the remainder |
Multiplication & Exponents
string * string |
CRASH! |
number * number |
math (multiply) |
string * number |
combines the strings multiple time |
string ** number |
CRASH! |
number ** number |
exponent(Math) |
Addition
string + string |
squishes them together |
string + number |
CRASH! |
number + number |
math(addition) |
Important
True or anything = |
True |
False and anything = |
False |
range(5) = |
[0,1,2,3,4] |
print("hello", "there") |
#displays hello there |
print("hello" + "there") |
#displays hellothere |
"hi" + "there" |
== "hithere" |
"hi" * 5 |
== "hihihihihi" |
while True: |
# forever |
While Loop with List:
thelist = [4, 3, 2, 1, 0]
index = 0 # start at the first item
while index < len(thelist):
print (thelist[index]) #prints each item
index = index + 1
|
For‐Loop with List:
forlist = [3, 4, 5, 2, 1]
for item in forlist:
print(item)
|
Example5-print out each item in list
mystr = "hello123"
numbers = [1,2,3,4,5,6]
print (numbers)
shoppinglist = ['shoes','bags','pants','shirts']
print (shoppinglist)
mixed = [1, 'hello', 2.5, True, False]
print (mixed)
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1
for myletterisawesome in mystr:
print(myletterisawesome)
for tientien in shoppinglist:
print(tiemtiem)
out = 0
for mrtim in shoppinglist:
out = out + 1
|
Example10-Def / function
def myprintnew(text, decoration):
print(decoration + str(text) + decoration)
return
myprintnew(1, "+++")
myprintnew('hello', '-=-=-=-=-=-=-=-=')
myprintnew(1, "@@@@@@@")
|
Example12-Circle area
def areaOfCircle (r):
if r <= 0:
return "Error: invalid radius"
pi = 3.1415
area = pi*r**2
return area
user_radius = float(input("Enter the radius: "))
print('The area of the circle is', areaOfCircle(user_radius))
|
Example11 - doubleit
def doubleit(number):
return number * 2
print (doubleit(3))
print (doubleit(doubleit(4)))
myvar = 12
myvar = doubleit(myvar)
myvar = doubleit(myvar)
print (myvar)
|
Example13.5- def printDefinitions + loop
def printDefinitions(word):
#variable
if word == "variable":
print ("""
A varible is value that can be change
""")
#function
elif word == "function":
print ("""
A function is block of quote can be reused
""")
#parameter
elif word == "parameter":
print ("""
A parameter is value that inside the blacket of the function
""")
#argument
elif word == "argument":
print ("""
A argument is value that inside the blacket of the function
""")
# function call
elif word == "function call":
print ("""
A function call is something that make the function run
""")
#string
elif word == "string":
print ("""
A string is list of character
""")
else:
print("unknown word")
return
while True:
user_input=input("Enter word")
printDefinitions(user_input)
|
Example17.5-The largest value from list
#write the function that returns the largest number in a list
#name: maxlist
#argument:list
#returns the largest value in the list
def maxlist(list):
maxvalue = list[0]
for item in list:
if item > maxvalue:
maxvalue = item
return maxvalue
mylist = [1,2,3,4,55,66,777,0,1]
print(maxlist(mylist))
|
Example18- Palindrome+loop
while True :
user_word = input("Enter your word")
if user_word != ("quit"):
print(len(user_word))
if user_word == ("quit"):
break
reverse = ""
letter_num = 0
while letter_num < len(user_word):
reverse = user_word[letter_num] + reverse
letter_num = letter_num + 1
if user_word == reverse:
print (user_word, "is palindrome")
else:
print (user_word, "is not palindrome")
|
|
|
Example1-Spelling a string out in reverse code
word = input("Type in an word: ")
reverse = ""
for letter in word:
reverse = letter + reverse
print ("Reverse: ", reverse)
|
Example2-Using boolean
print(True)
print (2<3)
print (2 != 2)
|
Example3-Countdown Code
user_number = input("Please enter a number: ")
number = int(user_number)
countdown_string = ""
while number > 0:
countdown_string = countdown_string + " " + str(number)
number = number-1
print (countdown_string)
|
Example4-Print Name
name = jaja YOOYUEN
print (name.upper()) --- JAJA YOOYUEN
print (name.lower()) --- jaja yooyuen
print (name.capitalize()) --- Jaja yooyuen
print (name.title()) --- Jaja Yooyuen |
Example6
word = input("What is the word ?")
reverse = ""
"""
letter_num = 0
while letter_num < len(word):
reverse = word[letter_num] + reverse
letter_num = letter_num + 1
"""
for letter in word:
reverse = letter + reverse
print ("Reverse: ",reverse)
out = 0
for letter in word:
out = out + 1
print(out)
|
Example7-Convert to binary
user_number = input("Please enter a number")
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)
|
Example9-Random and other
import random
inlist = (1,2,3,4,5,6,7)
random_int = random.choice(inlist)
print (inlist, '', random_int)
fplist = (1.1,2.1,3.1,4.1,5.1,6.1,7.1)
random_fp = random.choice(fplist)
print (fplist, '', random_fp)
strlist = ('love','captain','verymuch')
random_str = random.choice(strlist)
print (strlist, '', random_str)
mylist = (1,2,3,4,5,6,7,1.1,2.1,3.1,4.1,5.1,6.1,7.1,'love','captain','verymuch')
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)
|
Example14-reverse
reverse = ""
letter_num = 0
word = input('type in a word: ')
while letter_num < len(word):
reverse = word[letter_num] + reverse
letter_num = letter_num + 1
|
Example15-palindrome
#create a function that will ask user for a string
#and then say if that string is palindrome or not
reverse = ""
letter_num = 0
word = input('type in a word: ')
while letter_num < len(word):
reverse = word[letter_num] + reverse
letter_num = letter_num + 1
if word == reverse:
print ("It is palindrome")
else:
print ("It is not palindrome")
|
Example16- Area(Triangle) and volume (Prism)
#write a function that computes the area of triangle
#name: areOfTriangle
#parameter: b, h
#return: area
def areaOfTriangle(b, h):
if user_base <= 0:
return "Error: invarid radius"
if user_height <= 0:
return "Error: invarid radius"
area = b * h / 2
return area
user_base = float(input('Enter the base of the triangle: '))
user_height = float(input('Enter the height of the triangle: '))
print ('The area of triangle is', areaOfTriangle(user_base, user_height))
#write a function that computes the volume of a prism
#name: volumeOfPrism
#return: volume
def volumeOfPrism(b, h, l):
if user_length <= 0:
return "Error: invarid radius"
volume = b * h * l / 2
return volume
user_length = float(input('Enter the length of the prism: '))
print('The volume of the prism is',volumeOfPrism(user_base, user_height, user_length))
|
Example18.5
'''
Apisara Yooyuen Jaja 5861004, 1005
'''
def isPalindrome(word):
reverse = ""
letter_num = 0
while letter_num < len(user_word):
reverse = user_word[letter_num] + reverse
letter_num = letter_num + 1
if word == reverse:
return True
else:
return False
while True :
user_word = input("Enter your word: ")
word = len(user_word)
if user_word == ("quit"):
break
if isPalindrome(user_word):
print(word)
print (user_word, "is palindrome")
else:
print(word)
print (user_word, "is not palindrome")
|
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment