Commands/ Functions
print() |
displays information of the screen |
input() |
receives info from the the user |
float() |
converts a value to decimal |
int() |
integer; coverts a value to an integer |
str() |
string; coverts a value to a string |
# |
Comment; no effect |
Vocabulary
variable |
something that can change |
string |
a list of characters |
Integer number |
Whole number/ counting number |
Float number |
The number in decimal |
Syntax |
Grammar/ Structure of language |
Modulo |
Find the remainder |
Boolean |
True/False |
Example
Print (2) – integer
Print (2.5) – float
Print (“Hello”) – string
Print (mystr) – variable
Print (str,”Hi”,2,1.0) – commas
mystr = “Hi”
mystr – name
“Hi” – value can change
print (int(1.5)) – 1
print (int(“2”)) – 2
print(float(1)) - 1.0 anything to a float |
Math
== |
equal to |
!= |
no equal to |
< |
less than |
> |
more than |
<= |
less than or equal to |
>= |
more than or equal to |
% |
Modulo, Find the remainder |
Addition
string + string |
combines the strings together |
string + number |
crash |
number + number |
math (addition) |
Multiplication and Exponents
string*string |
CRASH! |
string*number |
combines the string multiple times |
number*number |
Multiply (Math) |
string ** number |
CRASH! |
string ** string |
CRASH! |
number ** number |
Exponent (Math) |
Naming Convention
Rule for giving name
- letter
- numbers
- underscore_
Valid name
- _myStr
- my3
- Hello_name
Invalid name
- 3my="hi" -- cannot start with number
- first name="hi"
- first-name |
Reverse word
word = input("please enter a word.")
"""
letter_num = 0
reverse = ' '
while letter_num < len(word) :
reverse = word[letter_num] + reverse
letter_num = letter_num + 1
"""
reverse = ' '
for letter in word:
reverse = letter + reverse
print ("Reverse: ",reverse)
|
Countdown Machine
while True:
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)
|
List
shoppinglist = ['coke zero', 'bacon', 'water', 'jelly', 'gummy bears']
print (shoppinglist)
print (shoppinglist[0])
list_num =0
while list_num < len(shoppinglist):
print ("List:", shoppinglist[list_num])
list_num = list_num + 1
for item in shoppinglist:
print (item)
numbers = range (1,6)
for num in numbers:
print (num)
# a string is a list of characters, letters, numbers etc.
mystr = "hello"
for letter in mystr:
print (letter)
shoppinglist = ['coke zero', 'bacon', 'water', 'jelly', 'gummy bears']
num = 0
for w in shoppinglist:
num = num + 1
print (num)
shoppinglist = ['coke zero', 'bacon', 'water', 'jelly', 'gummy bears']
num = 0
for w in shoppinglist:
num = num + 1
print (num)
|
|
|
Finding area of a circle
while True:
user_radius = input("Please enter the radius of the circle:")
radius = float(user_radius)
pi = 3.1415
area = (piradius*2)
print ("The area of the circle is", area)
|
Calculator program
def calc (num1, num2, operation):
if operation == "sum":
return sum (num1,num2)
elif operation == "product":
return product (num1,num2)
elif operation == "diff":
return diff (num1,num2)
elif operation == "div":
return div (num1,num2)
# use if/elif/else to check what operation to do
# call the correct function and return the answer
def sum (a, b):
return a+b
# calculate the sum of a and b
# return the answer
def product (a, b):
return a*b
# calcualate the product of a and b
# return the answer
def diff (a, b):
return a-b
# calculate the difference between a and b
# return the answer
def div (a, b):
if b!=0:
return a/b
else:
return ("Error")
# calculate the division of a and b
# return the answer
print (calc (10, 0, "div"))
print (calc (1, 2,"sum")) # output should be 3
print (calc (4, 2, "diff")) # output should be 2
print (calc (9, 3, "div" )) # output should be 3
print (calc (2 ,12, "product" )) # output should be 24
|
Finding the area of the triangle and its prism
#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 of the triangle:'))
def areaOfTriangle (base, height):
return 0.5baseheight #or 1/2
#functioncall
print ('The area of the triangle is', areaOfTriangle(user_base, user_height))
#write function compute volume of prism
#name: volumeOfPrism
#parameters: base, height, prism_height
#return volume
def volumeOfPrism (base,height,prism_height):
# area * prism_height
volume = areaOfTriangle (base,height) * prism_height
return areaOfTriangle (base,height) * prism_height
user_prism_height = float(input('Enter the height of the prism:'))
print ('The area of the prism is', volumeOfPrism (user_base,user_height,user_prism_height))
|
Maximum
# write a function that returns the largest of two values
# name: max2
# arguments: num1, num2
# return: the largest value
def max2 (num1,num2):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
return maxvalue
print (max2(3,4))
# write a function that returns the largest of three values
# 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
print (max3(3,4,8))
# write a 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
list = [1,2,3,6,19,50,2,4,5]
print (maxlist(list))
|
1. Multiplying number
#Receive input from the user as a float, and print out half of that number. e.g. user enters 12.5, print out 6.25
user_input = input("Please enter a number:")
number = float(user_input)
finalnumber = 0.5*number
print(finalnumber)
|
2. Output
#What is the output of the following code:
y = True
print (not y or 2<3)
#output is True
|
3. Error
message = "hello"
if (len(message) >5)
print ("Message too long")
else:
print ("Message is good")
line 3 has an error because it has no indent
|
4. Divisible by 3
#create a program to receive a number from the user and determine if that number
#is divisible by 3
user_input = input("Please enter a number:")
number = int(user_input)
if number %3 == 0:
print(number,"is divisible by 3")
else:
print (number, "is not divisible by 3")
|
|
|
5. Even Number
# print all the even numbers from 1 to 100 using a while loop
num = 2
while num <= 100:
print (num)
num = num+2
|
6. Output 2
#What is the output of the following code?
condition = True
number = 5
if condition == False: #False
number = number ** 2
elif number <5: #False
number = number * 2
elif condition == True: #True
number = number%2 #5%2=1
else:
number = number/2
print (number) # output = 1
|
7. my list (method 1)
#Given a list called mylist, print all elements from the list using a loop
mylist = ["Milly","Prim","Pizza"]
for item in mylist:
print (item)
|
7. List: while loop (method 2)
#while loop solution
mylist = [1,2,3,4,5]
num = 0
while num < len(mylist):
print (mylist[num])
num = num+1
|
9. Multiplication Table
#Write a function called multiplicationTable that asks the user for a number and
#computes its multiplication table.
def multiplicationTable ():
user_input = input ("Enter a number:")
num = int(user_input)
count=1
while count <= 10:
print (num,"",count,"=",numcount)
count = count+1
#function call
multiplicationTable()
|
1) Multiply 5
#Write a program that receives input from the user, converts it to an integer,
#and print the product of the integer and 5
user_input = input("Please enter a number.")
user_input = int(user_input)
product = user_input * 5
print (product)
|
2) Output
#What is the output of the following code?
x = False
print (x and True or 1 == 1)
#False and True = False
# False or True = True
# output is True
|
3) Error 2
#condisder the following code
def doubleValue(value):
return value*2
print (doubleValue(4))
#line 2 has error because it is not indented
|
4) Types of number
#write a program that receives a number from the user and determines if that
#number is negative,zero or positive.
user_input = int(input("Please enter a number:"))
if user_input > 0:
print (user_input,"is positive")
elif user_input == 0:
print (user_input,"is zero")
else:
print (user_input,"is negative")
|
5) Even numbers while loop
# write a program that prints all the even numbers from -100 to -1 using a
#while loop
num = -100
while num <= -2:
print (num)
num = num + 2
|
6) Output 2
#What is the output of the following code:
condition = 2<3 #True
number = 3
if condition != True: #True != True ;False
number = number ** 2
elif number <= 0: #3 <= 0 ;False
number = number * 3
elif number > 3: # 3>3 ; False
number = number%10
else: #everything is False do must do else
number = number - 1 * 2 # number - 2; 3-2=1; 1
print (number) # 1
|
8) 0......
#complete the program below by filling in the blank
# Expected output of program
# 0
# 01
# 012
# 0123
# 01234
mystring = ""
count = 0
while count <= 4:
mystring = mystring +str(count)
print (mystring)
count = count + 1
|
9) Area of Ellipse
# Write a function called areaOfEllipse () that computes the area of an ellipse
#using the equation pir1r2
# The function should be given 2 parameters (radius1 and radius 2) and should
#return the area
def areaOfEllipse (radius1,radius2):
pi = 3.1415
area = piradius1radius2
return area
#function call
area1 = areaOfEllipse(2,3)
print(area1)
|
11) Even and Odd
# Write a program that repeatly receives positive integers from the user. When
# the user enters a negative integer, exit the loop and print how many of the
# numbers entered were even and odd.
evencount = 0
oddcount = 0
while True:
user_input = int(input("Enter a number:"))
if user_input < 0:
print ("Evencount=", evencount)
print ("Oddcount=",oddcount)
break
elif user_input > 0:
if user_input % 2 == 0:
evencount = evencount + 1
else:
oddcount = oddcount + 1
|
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment