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)
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)
def sum (a, b):
return a+b
def product (a, b):
return a*b
def diff (a, b):
return a-b
def div (a, b):
if b!=0:
return a/b
else:
return ("Error")
print (calc (10, 0, "div"))
print (calc (1, 2,"sum"))
print (calc (4, 2, "diff"))
print (calc (9, 3, "div" ))
print (calc (2 ,12, "product" ))
|
Finding the area of the triangle and its prism
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
print ('The area of the triangle is', areaOfTriangle(user_base, user_height))
def volumeOfPrism (base,height,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
def max2 (num1,num2):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
return maxvalue
print (max2(3,4))
def max3 (num1,num2,num3):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
if num3 > maxvalue:
maxvalue = num3
return maxvalue
print (max3(3,4,8))
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
y = True
print (not y or 2<3)
|
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
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
num = 2
while num <= 100:
print (num)
num = num+2
|
6. Output 2
condition = True
number = 5
if condition == False:
number = number ** 2
elif number <5:
number = number * 2
elif condition == True:
number = number%2
else:
number = number/2
print (number)
|
7. my list (method 1)
mylist = ["Milly","Prim","Pizza"]
for item in mylist:
print (item)
|
7. List: while loop (method 2)
mylist = [1,2,3,4,5]
num = 0
while num < len(mylist):
print (mylist[num])
num = num+1
|
9. 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
multiplicationTable()
|
1) Multiply 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
def doubleValue(value):
return value*2
print (doubleValue(4))
|
4) Types of number
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
num = -100
while num <= -2:
print (num)
num = num + 2
|
6) Output 2
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......
mystring = ""
count = 0
while count <= 4:
mystring = mystring +str(count)
print (mystring)
count = count + 1
|
9) Area of Ellipse
def areaOfEllipse (radius1,radius2):
pi = 3.1415
area = piradius1radius2
return area
area1 = areaOfEllipse(2,3)
print(area1)
|
11) 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