Function
print() |
Show the information that you want on screen |
int() |
Change number to be number integer |
float() |
Change number to be decimal number |
input() |
Gain information from user |
str() |
A list of number, letter and symbols |
len() |
The length of the string |
# |
Comment, no effect |
Multiplication and Exponent
string*number |
combine that string |
string*string |
CRASH |
number*number |
Multiply |
string**string |
CRASH |
number**number |
Exponent |
string**number |
CRASH |
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)
|
Sort word per line
mystr = "Hello"
letter_num = 0
while letter_num<len(mystr):
print(mystr[letter_num])
letter_num=letter_num+1
H
E
L
L
O
|
Print Name
name="tim GIRARD"
print(name.upper())
|
area of circle
def areaOfcircle(radius):
if radius<=0:
return "Error invalid radius"
pi=3.1415
area = piradius*2
return area
user_radius = float(input("Enter the radius: "))
print ("The area of the circle is",areaOfcircle(user_radius))
|
Enter the radius: 0
The area of the circle is Error invalid radius
>>>
Enter the radius: 7
The area of the circle is 153.9335
doublet
def doublet(number):
return number * 2
print (doublet(3))
print (doublet(doublet(4)))
|
function call
def printDefinitions(word):
if word == "variable":
print ("""A variable is the value that can be changed""")
elif word == "function":
print ("""A function is blog of code that we can reuse""")
elif word == "parameter":
print ("""A parameter is something is given in the function""")
elif word == "arguement":
print ("""A arguement is something is given in the function""")
elif word == "function call":
print ("""A function call is something that make the function runs""")
elif word == "string":
print ("""A string is character such as symbol, number""")
else:
print("unknown word")
return
while True:
user_input=input("Enter word:")
printDefinitions(user_input)
|
Enter word:variable
A variable is the value that can be changed
Enter word:function
A function is blog of code that we can reuse
Enter word:hello
unknown word
Enter word:
Maxlist
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))
|
areaOfTriangle,volumeOfPrism
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))
def volumeOfPrism(b,h,l):
volume = areaOfTriangle(b,h)*l
return(volume)
user_base = float(input('Enter the base of the prism'))
user_height = float(input('Enter the height of the prism'))
user_length = float(input('Enter the length of the prism'))
print('The volume of the Prism is',volumeOfPrism(user_base,user_height,user_length))
|
Enter the base of the triangle6
Enter the height of the triangle6
The area of the triangle is 18.0
Enter the base of the prism6
Enter the height of the prism6
Enter the length of the prism6
The volume of the Prism is 108.0
|
|
Addition
string+string |
Combine together |
string+number |
CRASH |
number+number |
Addition |
Vocabulary
variable |
Hold a value and can be changed |
string |
A list of character such as number,letter and symbol |
integer number |
whole number/ counting number |
float number |
the number in decimal |
syntax |
Grammar/Structure of language |
Boolean |
True/False |
Math
== |
equal to |
!= |
no equal to |
< |
less than |
> |
more than |
<= |
less than or equal to |
>= |
more than or equal to |
% |
Modulo, Find the remainder |
Countdown Machine
user_number=input("What number do you want to count down?")
numer=int(user_number)
countdown_string=' '
while number > 0
countdown_number= countdown_string+
str(number) + " "
number=number-1
#print(number)
print(countdown_string)
|
Naming Cinvention
Rule for giving name |
-letter |
-numbers |
-underscore |
Valid name |
-myStr |
-my3 |
-Hello_there |
Invalid name |
-3my="hi" -- cannot start with number |
-first number='hi' |
-first-name |
-first+name |
random stuff
import random
intlist= [1,2,3,4,5]
random_int = random.choice(intlist)
print(intlist,random_int)
fplist=[1.1,2.2,3.3,4.4,5.5]
random_fp= random.choice(fplist)
print(fplist,random_fp)
strlist=['opal','love','tientien']
random_str= random.choice(strlist)
print(strlist,random_str)
mylist=['tientien',1,5.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)
|
[1, 2, 3, 4, 5] 4
[1.1, 2.2, 3.3, 4.4, 5.5] 4.4
['opal', 'love', 'tientien'] opal
['tientien', 1, 5.5] tientien
[1, 2, 3] 3
>>>
My print
def myprint(text):
print(""+str(text)+"")
return
myprint (1)
myprint ("hello")
myprint (2.5)
def myprintnew(text, decoration):
print(decoration + str(text) + decoration)
return
myprintnew(1,"+++")
myprintnew ('hello','=-=-=-=-=-=-=-=')
|
reverse
reverse = ""
letter_num=0
user_input = input("type in a number")
while letter_num<len(user_input):
reverse = user_input[letter_num]+reverse
letter_num = letter_num+1
if reverse == user_input:
print ('parindorm')
else:
print ('not parindorm')
|
type in a number12321
parindorm
>>>
type in a number
3
parindorm
>>>
type in a number13
not parindorm
>>>
maxvalue
def max2(num1,num2):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
return maxvalue
print (max2(4,5))
print (max2(33,5))
def max3(num1,num2,num3):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
if num3 > maxvalue:
maxvalue = num3
return maxvalue
print (max3(1,2,3))
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment