Vocabulary
Variable |
Hold a value and can be change |
String |
A list of character such as number, letter andsymbols |
Integer Number |
Whole number/counting number |
Float Number |
The number in decimal |
Syntax |
Grammar /structure |
Modulo |
Find the remainder |
Boolean |
true/False |
Addition
string+string |
Combine together |
string+number |
CRASH! |
Number+number |
Addition(Math) |
Reverse
#Finish this program so that it gets a word from the user and prints
#that word backwards
reverse = "" #do not change
letter_num = 0 #do not change
word = input("Please enter a word: ")#get a word from the user
'''
while letter_num < len(word):#compare the letter_num to the lenght of the word
reverse = word[letter_num]+reverse#kepp adding the letter to the front of reverse
letter_num = letter_num+1#go to the next letter in the word
'''
for lette in word :
reverse = letter + revers
print ("Reverse: ",reverse)
#creating list
mylist = [1,2,3,4,5,6]
mylist2 = ['hi', 'hello','anything']
mylist3 = [1, 'hello', 2.5] |
Radius of Circle
while True:
#Ask the user for a radius of a circle
user_radius = input("Please enter the radius of the circle")
#Convert the given radiusto a floating point
radius = float(user_radius)
#make a variable called pi
pi = 3.1415
#Calculate the area of the circle using exponents
area = pi radius *2
#display the area of the circle to the user
print("The area of the circle is", area) |
A multiple string
# write definitions for the following words and print them using
# a multi-line string
def printDefinitions(word): # parameter word
if word == "variable":
#variale
print """
A variable is ...
"""
elif word == "function":
# function
print ("""
A function is ...
""")
elif word == "parameter":
print("""
A parameter is ...
""")
elif word == "argument":
print("""
A argument is ...
""")
elif word == "string":
print("""
A srting is ...
""")
elif word == "function call":
print("""
A function call is ....
""")+
# parameter
# argument
# string
# function call
else:
return "unknown word"
#ask the user for the name of the word define
user_input = input ("
printDefinitions( user_input ) |
How to make list in python
#how to make list in python
shoppinglist = ['bag', 'shoes', 'boots', 'shiryt']
print(shoppinglist[2])
item_number = 0
#while loop
while item_number < len(shoppinglist):
print ("List item:",shoppinglist[item_number])
item_number = item_number + 1
#for loop
out = 0
for muids in shoppinglist:
out = out + 1
#print("list item:", muids)
print (out) |
Palindrome
def isPalindrome(word):
index = 0
reverse = ''
for letter in word:
reverse = letter + reverse
if reverse == word:
return True
elif word != reverse:
return False
while True:
user_input = input("Please enter a word: ")
if user_input == ("quit"):
break
print (len(user_input))
check = (isPalindrome(user_input))
if check == True:
print(user_input,"is a palindrome")
elif check == False:
print (user_input,"is not a palindrome") |
|
|
Function
print() |
Show information that you want toscreen |
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 the string |
string*string |
CRASH! |
number*number |
Multiply(math) |
string**string |
CRASH! |
number**number |
Exponent(math) |
string**number |
CRASH! |
Random
import random
# Create a list of integers
inlist = [1,2,4,5,7,9]
random_int = random.choice(intlist)
print (inlist, random_int) #print the entire list andthe random item
# Create a list of floating point numbers
fplist = [1.5,2.2,1.0,100.999]
random_fp = random.choice(fplist)
print (fplist, random_fp) #print the entire list and the random item
# Create a list of strings
strlist = ['dog', "cat", 'match', "it's me", '"hi"']
random_str = random.choice(strlist)
print (strlist, random_str) #print the entire list and the random item
# Create a list of integers and floating point numbers and string
mylist = [1,2,2.2,3.2, 'string', "hi"]
random_item = random.choice(mylist)
print (mylist, random_item) #print the entire list and the random item
# create alist of following variable
myvar1 = 1
myvae2 = 2
myvar3 = 3
varlist = [myvar1, myvar2, myvar3]
random_var = random.choice(varlist)
print (varlist, random_var) #print the entire list and the random item |
Countdown
# Create a program that receives a number from the user and counts down
# from that number on the same line
# receive the number from the user as a string
user_number = input("7")
#convert the user number to an integer
number = int(user_number)
#setup the countdown string
countdown_string = '7 6 5 4 3 2 1 0'
while number > 0:
# add the number to the string
countdowm_string = something + str(somethingelse)
# subtract 1 from the number
number = number - 1
print (countdown_string) |
How to create function
# how to create a function
def nameOfFunction(myvar1, myvar2): #parameters or argument
#write a function
# name : areaOfTriangle
# parameters : base height
# return: area
user_base = float(input('Enter the base of the triangle: '))
user_height = float(input('Enter the height ofthe triangle: '))
print ('the area of the triangle is', areaOfTriangle(user_base, user_hight))
# name: volumeOfPrism
# parameters: area height
# return: volume
def volumeOfPrism
user_prism_height = float(input9'Enter the height of prism: '))
print('the volume of the prism is', volumeOfPrism(areaOfTriangle(user_base, user_height), user_prism_height)) |
Operation
def calc(num1, num2, operation):
#user if/elif/else to check what operation
if operation == "sum":
return sum(num1, num2)
elif operation == "div":
return div(num1, num2)
elif operation == "product":
return product (num1, num2)
else:
print ("unknown operation")
def sum(a, b):
#calculate the sum of a and b
return a+b
#return the answer
def product(a, b):
# calculate the productof a and b
return a * b
#return the answer
def diff(a, b):
# calculate the difference between a and b
return a -b
# return the answer
def div(a, b):
# calculate the division of a and b
return a / b
# return the answer3
print(calc ( 10, -2, "div"))
print(calc(1,2,"sum")) #output should be 3
print(calc (4, 2, "diff")) # output should be 2
calc (9, 3, "div" )) #output should be 3
calc (2, 12, "product")) #output shouldbe 24 |
|
|
Math
== |
equal to |
!= |
no equal to |
< |
less than |
> |
more than |
<= |
less than or equal to |
>= |
more than or equal to |
% |
Modulo, find the remainder |
Convert Binary
#write a program that convert a number to binary
while True:
#get a number from the user
user_number = input("please enter the number")
#convert to integer
number = int(user_number)
binary_string = ''
while (number > 0):#the number is greater than 0)
remainder = number % 2#user Modulo %
binary_string = str(remainder) + binary_string #remainder + binary string
number = number // 2#must use // when you divide
#after the loop print the binary string
print ("Binary string is",binary_string)
#expected output - 5 = 101
#expected output - 3 = 11
#expected output - 2 = 10 |
Convert Hexadecimal
#write a program that convert a number to binary
while True:
#get a number from the user
user_number = input("please enter the number")
#convert to integer
number = int(user_number)
hex_string = ''
while (number > 0):#the number is greater than 0)
remainder = number % 16#user Modulo %
if remainder == 10:
remainder = 'A'
elif remainder == 11:
remainder = 'B'
elif remainder == 12:
remainder = 'C'
elif remainder == 13:
remainder = 'D'
elif remainder == 14:
remainder = 'E'
elif remainder == 15:
remainder = 'F'
hex_string = str(remainder) + hex_string #remainder + hexadecimal string
number = number // 16#must use // when you divide
#after the loop print the Hexadecimal string
print ("Hexadecimal string is 0x" + hex_string)
#expected output - 5 = 101
#expected output - 3 = 11
#expected output - 2 = 10 |
Return Max number
def max2(num1, num2):
if num1 > num2:
return num1
else:
return num2
def max3(num1, num2, num3):
if num1 > num2 and num1 > num3:
return num1
elif num2 > num1 and num2 > num3:
return num2
else:
return num3
print (max2(10, 15))
print (max2(20, 10))
print (max3(1, 2, 3))
print (max3(15, 20, 10))
print (max3(99, 15, 47)) |
My list
mylist = ['lion' , 'tiger', 'cheetah', 'cougar' ,'lynx']
print (mylist[1])
print (mylist)
user_guess = input("Guess a word: ")
random_item = random.choice(mylist)
print (random_item)
if user_guess == random_item:
print ("Correct guess")
else:
if user_guess in mylist:
print ("yes, in the list")
else:
print ("No,not in the list") |
Multiple Parameter
_var1 = 1
_var1 = 3
_var1 + 100
print(_var1)
def bacon () :
print("hello it'sbacon")
print("line 2")
print("line 3")
print("line 4")
print("line 5")
print("line 6")
print("line 7")
print("line 8")
return
def myprint(text): #Single parameter
print("" + str(text) + "")
return
myprint(1)
myprint("hello")
myprint(1+2)
def myprint2(text, decoration): #multiple parameters
print (decoration + str(text) + decoration)
return
myprint2(12312321312, "+++")
myprint2("hello","<<>>")
def doubleIt(number):
return number * 2
myvar = 2
myvarDouble = doubleIt(myvar)
print(myvarDouble)
print(doubleIt("hello"))
myvar = doubleIt(doubleIt(3)) # same as doubleIt(6)
print(myvar)
def sumIt(num1, num2):
return num1+num2
print(sumIt("a", "b"))
print (sumIt(2,3))
def areaOfCircle (r):
pi = 3.1415
area = pi r * 2
return
user_Radius = input('Enter the radius:')
radius = float(user_radius)
print("the area of the circle is", areaOfCircle(radius)) |
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment