Math of Symbols
== |
Equal to |
!= |
No equal to |
< |
Less than |
> |
More than |
<= |
Less than or equal to |
>= |
More than or equal to |
% |
Modulo, find the remainder |
+ |
Add |
- |
Subtract |
* |
Multiplication |
** |
Exponent |
/ |
Divide and quotient is float |
// |
Divide and quotient is integer |
Text
single quoted |
'example' |
double quoted |
"example" |
Functions
print() |
display information on the screen |
input() |
display information from the user |
int() |
convert a value to an integer |
len() |
The length of the string |
float() |
Change number to be decimal number |
str() |
converts the value to a string |
# |
Comment, no effect |
What's is your name? code
mystring = "hello"
print (mystring)
firstname = input( "what is your first name?")
lastname = input( "what is your last name?")
fullname = firstname + " " + lastname
print (fullname)
letternumber = int(input( " what is letter number? " ))
if letternumber >len(fullname):
print ( " invalid letter number, try again! " )
else:
letter = ( fullname[letternumber] )
print (letter)
numberletter = int(input( "how many times to print letter " ))
if numberletter >100:
print ( " too many letters to print! " )
else:
print (letter * numberletter )
|
hello
what is your first name?
what is your last name?
what is letter number?
how many times to print letter
List Random
import random
intlist = [1,10,100]
random_int = random.choice(intlist)
print(random_int,intlist)
fplist = [2,20,200]
ramdom_fp = random.choice(fplist)
print(ramdom_fp,fplist)
strlist = ('Pin','Anpan','Bella')
ramdom_str = random.choice(strlist)
print(ramdom_str,strlist)
mylist = [1,10,100,2,20,200,'Pin','Anpan','Bella']
ramdom_item = random.choice(mylist)
print(ramdom_item,mylist)
myvar1 = 1
myvar2 = 2
myvar3 = 3
varlist = [myvar1,myvar2,myvar3]
random_var = random.choice(varlist)
print(random_var,varlist)
|
10 [1, 10, 100]
200 [2, 20, 200]
Bella ('Pin', 'Anpan', 'Bella')
2 [1, 10, 100, 2, 20, 200, 'Pin', 'Anpan', 'Bella']
3 [1, 2, 3]
bacon()
def bacon():
print("hello it's bacon")
print("line 2")
print("line 3")
print("line 4")
return
bacon()
bacon()
bacon()
|
hello it's bacon
line 2
line 3
line 4
hello it's bacon
line 2
line 3
line 4
hello it's bacon
line 2
line 3
line 4
myprintnew (text, decoration)
def myprintnew (text, decoration):
print(decoration + str(text) + decoration)
return
myprintnew(1, "+++")
myprintnew('hello','-=-=-=-=-=-=-=-=')
myprintnew(1, "@@@@@@@")
|
+++1+++
-=-=-=-=-=-=-=-=hello-=-=-=-=-=-=-=-=
@@@@@@@1@@@@@@@
area of a triangle
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))
def volumeofPrism(b,h,l):
volume = areaofTriangle(b, h) *l
return volume
user_lenght = float(input('Enter the length of the prism: '))
print('The volume of the prism is',volumeofPrism(user_base, user_height, user_lenght) )
|
Enter the base of the triangle:11111
Enter the height of the triangle:2222
The area of the triangle is 12344321.0
Enter the length of the prism: 3333
The volume of the prism is 41143621893.0
|
|
Vocabulary
Variable |
Hold a value and can be changed |
String |
A list of characters such as number, letters, symbols |
Integer number |
Whole number/ counting number |
Float number |
The number in decimal |
Syntax |
Grammar / Structure of language |
Modulo |
Find the remainder |
Boolean |
True / False |
Naming Conventions
Rules for naming variables:
- letters
- numbers
- underscores (_)
- can start with letters or underscores ONLY
- NO SPACES
Valid names:
- _mystr
- my3
Hello_there
Invalid names:
- 3my= "hi" -- cannot start with number
- first name = "hi" -- no spaces allowed
- first-name -- dashes are not accepted |
Example
Print (2) – integer
Print (2.5) – floating point
Print (“Hello”) – string
Print (mystr) – variable
Print (mystr,”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
Modulo/Remainder %
print (4%2) → 0
print (30%7) → 2
|
A radius of a circle code
#Ask the user for a radius of a circle
user_radius =input("What is the radius of the circle?")
#Convert the given radius to a floating point
radius = float(user_radius)
#make a variable called pi
pi = 3.1415
#Calculate the area of the circle using exponents
area = piradius*2
#display the area of the circle to the user
print("The area of the circle is" ,area) |
What is the radius of the circle?123
The area of the circle is 47527.753500000006
Reverse Code
word =input("Please enter your name: ")
index = 0
reverse =''
while index < len(word):
reverse = word[index]+ reverse
index = index + 1
print ("Reverse: ",reverse) |
Please enter your name: Timmy
Reverse: ymmiT
myprint(text)
def myprint(text):
print("" + str(text) + "")
return
myprint(1)
myprint("hello")
myprint(2.5)
|
areaOfCircle(r)
def areaOfCircle(r):
if r <= 0:
return "Error: invalid radius"
pi = 3.1415
area = pi r * 2
return area
user_radius = float(input("Enter the radius:"))
print ('The area of the circle is', areaOfCircle(user_radius))
|
Enter the radius:300
The area of the circle is 282735.0
Enter the radius:0
The area of the circle is Error: invalid radius
_var1
_var1 = 1
_var1 = 3
_var1 + 100
print(_var1)
|
maxlist
def maxlist(list):
maxvalue = list[0]
for item in list:
if item > maxvalue:
maxvalue = mylist
return maxlist
mylist = [21365741,2135416,2,54131,1.1515]
print(maxlist(mylist))
|
maxvalue
def max2(num1,num2):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
return maxvalue
print('The largest number is',max2(2,3))
print('The largest number is',max2(12222,10))
def max3(num1,num2,num3):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
if num3 > maxvalue:
maxvalue = num3
return maxvalue
print('The largest number is',max3(1,5,10))
print('The largest number is',max3(12222,5,10))
print('The largest number is',max3(12222,164.3415645,121348561240))
|
The largest number is 3
The largest number is 12222
The largest number is 10
The largest number is 12222
The largest number is 121348561240
|
|
Multiplication and Exponents
string * number |
combine that string multiple times |
string * string |
crash |
number * number |
math - multiply |
string ** string |
crash |
number ** number |
math - multiply |
string ** number |
crash |
Addition
string + string |
combine together |
string + number |
crash |
number + number |
math-addition |
Conditionals
If..... :then..... else....... |
If the statement is true then do command under then else do command under else |
while...... |
While this is true loop the command under the conditiona |
While True |
loops forever |
for each item in name of list |
For every item in the list repeat the command under the loop that many times. (a string is a list too) |
Big or small code
mystr = "hello THERE"
print (mystr.upper())
print (mystr.lower())
print (mystr.capitalize())
print (mystr.title())
|
HELLO THERE
hello there
Hello there
Hello There
Please enter a number Code
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)
|
Sort word per line
mystr = "Hello"
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1 |
Shoping List code
shoppinglist = ['shoes', 'bags', 'shirts', 'pants']
index = 0
while index < len(shoppinglist):
print (shoppinglist[index])
index = index + 1
for item in shoppinglist:
print (item) |
shoes
bags
shirts
pants
shoes
bags
shirts
pants
printDefinitions
def printDefinitions(word):
if word =="variable":
print ('....')
elif word =="function":
print ('....')
elif word =="parameter":
print ('....')
elif word =="argument":
print ('....')
elif word =="function call":
print ('....')
elif word=="string":
print ('....')
else:
print("unknown word")
return
while True:
user_input = input("Enter word: ")
printDefinitions(user_input)
|
area of a triangle
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))
def volumeofPrism(b,h,l):
volume = areaofTriangle(b, h) *l
return volume
user_lenght = float(input('Enter the length of the prism: '))
print('The volume of the prism is',volumeofPrism(user_base, user_height, user_lenght) )
|
Enter the base of the triangle:11111
Enter the height of the triangle:2222
The area of the triangle is 12344321.0
Enter the length of the prism: 3333
The volume of the prism is 41143621893.0
doubleIt(number)
def doubleIt(number):
return number*2
print (doubleIt(3))
print (doubleIt(doubleIt(4)))
myvar = 12
myvar= doubleIt(myvar)
myvar= doubleIt(myvar)
print(myvar)
|
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment