|
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
|
|
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
|
|
input
|
receives information from user
|
|
print
|
show the result
|
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))
def bacon() :
print ("hello it's bacon")
return
bacon()
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")
#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))
#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))
mylist = [1,2,3,4,5]
for number in mylist :
print (number)
number = -100
while number < -1 :
print (number)
number = number + 2
|
==
|
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
|
|
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
|
mystr = "Hello"
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1
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)
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)
#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))
myword = "hellothere"
print (myword[4])
mystring = ""
count = 0
while count < 5 :
mystring = mystring + str(count)
print (mystring)
count = count + 1
|
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
|
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)
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)
number = input ("What you want to countdown :")
number = int(number)
countdown = ""
while number > 0:
countdown = countdown + str(number) + " "
number = number - 1
print(countdown)
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(radius^2^) #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
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)
# write a function that computers the area of a triangle
#name: areaofTriangle
#parameters : b, h
#return : area
def areaofTriangle(b,h):
area = 0.5*b*h
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))
# write a function that computers the area of a triangle
#name: areaofTriangle
#parameters : b, h
#return : area
def areaofTriangle(b,h):
area = 0.5*b*h
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))
wlist = [2,4,5,6,7,8]
letternum = 0
while letternum < len(wlist) :
print (wlist[letternum])
letternum = letternum + 1
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.