Abbreviation
str |
string |
len() |
lenght |
print() |
print |
int() |
change to be integer |
float() |
make it to be decimal number |
while : |
While something is true the condition in the loop will proceed |
for ...... in...... : |
For ..... in (your list or string) repeat the command inside the loop that many times |
while True : |
forever proceed |
If ........: then....... else |
If the "If" statement is true the loop will proceed the condition inside then loop but if the "If" statement is false the loop will proceed the condition inside else loop |
Vocabulary
variable |
a value or thing that can be change |
string |
A list of character such as letter, number or symbol |
boolean |
True and False (with capital letter) |
modulo |
Remainder of the division |
syntax |
The grammar of writing pyton |
float |
number with decimal point |
integer |
Rounded number with no decimal point |
function
input |
receives information from user |
print |
show the result |
Rule for giving name
- letter
- number
- underscore
- NO SPACE!!!!!!
- start with letter or underscore ONLY
Valid name
- myvar_1
- myvar1_
- _myvar1
Invalid name
- 1myvar (number be the first letter of the name)
- my var (no space in the name)
- my-var (no dash) |
Define the function
def areaofcircle (radius): #define function named areaofcircle, parameter radius
if radius <= 0:
return "Error: Invalid radius"
pi = 3.1415
area = 3.1415 * radius**2
return area # return the area of the circle
userradius = float(input("Enter the radius:"))
print ("The area of the circle is", areaofcircle(userradius))
|
The result
Enter the radius:0
The area of the circle is Error: Invalid radius
Define Function
def bacon() :
print ("hello it's bacon")
return
bacon()
|
Function of Palindrome
string = input("Please type the string:")
string = str(string)
letter_num = 0
reverse = ""
while letter_num < len(string) :
reverse = string[letter_num]+ reverse
letter_num = letter_num + 1
if string == reverse :
print ("This string is palindrome")
else :
print ("This string is not palindrome")
|
Result of the function :
Please type the string:456
This string is not palindrome
OR
Please type the string:12321
This string is palindrome
Maximum Value
#write a function that returns the largest of two values
#name: max2
#arguments: num1, num2
#return: the largest value
def max2(num1, num2):
if num1>num2 :
maxvalue = num1
else :
maxvalue = num2
return maxvalue
user_num1 = int(input("Enter the first number:"))
user_num2 = int(input("Enter the second number:"))
print ("The largest value is:",max2(user_num1, user_num2))
|
Enter the first number:5
Enter the second number:2
The largest value is: 5
Maximum three function
#write a function that returns the largest number of three value
#name: max3
#arguments: num1, num2, num3
#return: the largest value
def max3 (num1,num2,num3):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
if num3 > maxvalue:
maxvalue = num3
return maxvalue
user_num1 = int(input("Enter the first number:"))
user_num2 = int(input("Enter the second number:"))
user_num3 = int(input("Enter the third number:"))
print ("The largest value is:",max3(user_num1, user_num2, user_num3))
|
Enter the first number:12
Enter the second number:3
Enter the third number:456
The largest value is: 456
For loop
mylist = [1,2,3,4,5]
for number in mylist :
print (number)
|
Even number from -100 to -1
number = -100
while number < -1 :
print (number)
number = number + 2
|
|
|
Operator
== |
compare |
!= |
not equal |
while |
loop |
+ |
plus |
- |
minus |
/ |
divide and quotient is float |
>= |
greater than or equal |
> |
greater than |
<= |
less than or equal |
< |
less than |
% |
keep the remainder |
** |
power |
# |
comment |
// |
divide and quotient is integer |
* |
multiply |
"""........""" |
multi-line comment |
Capitalize
print (mystr.upper()) |
all letter become uppercase |
HELLO THERE |
print (mystr.lower()) |
all letter become lowercase |
hello there |
print (mystr.capitalize()) |
first letter become uppercase, all other lowercase |
Hello there |
print (mystr.title()) |
first letter of each word is uppercase |
Hello There |
Put letter in different line
mystr = "Hello"
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1
|
Different type of list
import random
intlist = [1,2,3]
random_int = random.choice(intlist)
print (intlist,random_int)
fplist = [1.02,3.02,5.36]
random_fp = random.choice(fplist)
print (fplist, random_fp)
strlist = ['mind','mom','hall']
random_str = random.choice(strlist)
print (strlist, random_str)
mylist = [1,2.35,'tiger']
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)
|
[1, 2, 3] 2
[1.02, 3.02, 5.36] 5.36
['mind', 'mom', 'hall'] mom
[1, 2.35, 'tiger'] tiger
[1, 2, 3] 2
Function defeinition
def printDefinition(word):
#word = user_input
if word=="variable":
print ("""
A variable is the value that can change. You can refered it by make the name of the variable
""")
elif word=="function":#function
print ("""
A function define the block of code that can be reuse
""")
elif word=="parameter":#parameter
print("""
A parameter is the thing that you give to the function in Pyton
""")
elif word=="argument":#argument
print("""
An argument is the thing that you give to the function
""")
elif word=="function call":#function call
print("""
A function call is command that call code in the function to run or execute
""")
elif word=="string":#string
print("""
A string is the list of letter, number, space or everything
""")
else :
print ("Unknow word")
return
while True:
user_input = input("Enter word:")
printDefinition(user_input)
|
RESULT
Enter word:funciton
Unknow word
Enter word:function
A function define the block of code that can be reuse
Enter word:hi
Unknow word
Enter word:hello
Unknow word'
Enter word:edlfw
Unknow word
Enter word:variable
A variable is the value that can change. You can refered it by make the name of the variable
Enter word:funciton call
Unknow word
Enter word:
The program keep asking to enter the word because the loop while True
Maximum number in list
#write a function that returns the largest number in a list
#name: maxlist
#argument: numlist
#return the largest value in a list
def maxlist(numlist):
maxvalue = numlist[0]
for item in numlist :
if item >= maxvalue:
maxvalue = item
return maxvalue
numlist = [1,2,35,2654,232,5,2,5]
print(maxlist(numlist))
|
Print fifth character from the list
myword = "hellothere"
print (myword[4])
|
expected output of the program
mystring = ""
count = 0
while count < 5 :
mystring = mystring + str(count)
print (mystring)
count = count + 1
|
Result must be:
0
01
012
0123
01234
|
|
Operation
string + string |
combine together |
string * string |
invalid syntax |
string * number |
repeat the string by the number |
number + number |
addition |
number * number |
multiple |
string ** string |
invalid syntax |
string ** number |
invalid syntax |
Reverse
word = input ("Please type the world : ")
letter_num = 0
reverse = ""
while letter_num < len(word) :
reverse = word[letter_num]+ reverse
letter_num = letter_num + 1
print ("reverse :", reverse)
OR
word = input("Please type the word :")
reverse = ""
for letter in word :
reverse = letter + reverse
print ("reverse :", reverse)
|
Convert decimal to binary
number = input ("What you want to convert to binary :")
number = int(number)
binary = ""
while (number > 0):
remainder = number%2
binary = str(remainder)+ binary
number = number//2
print (binary)
|
Countdown number
number = input ("What you want to countdown :")
number = int(number)
countdown = ""
while number > 0:
countdown = countdown + str(number) + " "
number = number - 1
print(countdown)
|
Circle area
user_radius = input("What is a radius of a circle?") # to get number from user
radius = float(user_radius) #Convert the given radius to a floating point
pi = float(3.1415) #determine the value of variable called pi
area = pi(radius2) #Calculate the area of the circle using exponents
print ("The area of the circle is", area) #Show the area of the circle to the user
|
Guessing Game
chance = 5
score = 0
mylist = ['coke','bacon', 'chicken', 'pocky', 'pepsi', 'pizza']
import random
random_item = random.choice(mylist)
while chance > 0:
print ("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
print ("Guessing Game")
print ("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
print ("Words:", mylist)
user_guese = input("Guese the word: ")
if user_guese == random_item:
score = score+100
print ("That's correct! Score:", score)
random_item = random.choice(mylist)
else:
chance = chance-1
if user_guese in mylist:
print ("Sorry, wrong choice!")
print ("Chances Remaining:", chance)
else:
print ("Sorry, that is not ever in the list")
print ("Chances Remaining:", chance)
print ("Game Over! The word was", random_item)
print ("Final Score:", score)
|
Area of triangle
# write a function that computers the area of a triangle
#name: areaofTriangle
#parameters : b, h
#return : area
def areaofTriangle(b,h):
area = 0.5bh
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 the triangle is", areaofTriangle(user_base,user_height))
|
RESULT
Enter the base of the triangle:6
Enter the height of the triangle:10
The area of the triangle is 30.0
>>>
Function of volume
# write a function that computers the area of a triangle
#name: areaofTriangle
#parameters : b, h
#return : area
def areaofTriangle(b,h):
area = 0.5bh
return area
user_base = float(input("Enter the base of the triangle:"))
user_height = float(input("Enter the height of the triangle:"))
#write a function that computes the volume of a prism
#name: volumeofPrism
#parameters: b, h, l
#return: volume
def volumeofPrism(b,h,l):
volume = areaofTriangle(b,h)*l
return volume
user_lenght = float(input("Enter the lenght of the prism:"))
print ("The volume of the prism is", volumeofPrism(user_base,user_height,user_lenght))
|
RESULT
Enter the base of the triangle:4
Enter the height of the triangle:6
Enter the lenght of the prism:10
The volume of the prism is 120.0
While loop
wlist = [2,4,5,6,7,8]
letternum = 0
while letternum < len(wlist) :
print (wlist[letternum])
letternum = letternum + 1
|
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment