|
Variables
|
a value that can change, depending on conditions or on information passed to the program.
|
|
String
|
list of characters, symbols and could also have numbers.
|
|
Syntax
|
structure of a program.
|
|
Print
|
to display a value on the screen
|
|
Loop
|
when the instructions repeat over and over.
|
|
Integer Number
|
Whole number/ counting number
|
|
Float Number
|
The number in decimal
|
|
Modulo
|
Used to finds the remainder
|
|
Boolean
|
True/False
|
|
List
|
Writing consecutive words, numbers down, one below the other in []
|
|
Algorithm
|
A list of steps to finish something. Instructions that can be performed with or without a computer.
|
|
Code
|
Commands created to allow computer to perform the functions.
|
|
string + string
|
Combines that strings together (squished together)
|
|
string + number
|
Crash!
|
|
number + number
|
Addition (Math)
|
import random
intlist = [1,2,3,4,5]
random_int = random.choice(intlist)
print (intlist, random_int)
fplist = [1.0,2.0,3.0,4.0,5.0]
random_fp = random.choice (fplist)
print (fplist, random_fp)
strlist = ["candy", "marshmellow", "icecream", "lollipop"]
random_str = random.choice (strlist)
print (strlist, random_str)
mylist =[1,2,3, 5.0,6.0,7.0, "Candy", "Icecream", "Gummybears"]
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)
while True:
user_number = input ("Enter a number")
number = int (user_number)
countdown_string = ""
while number > 0:
countdown_string = countdown_string + str(number)
number = number - 1
print (countdown_string)
#write a function
#name: areaofTriangle
#parameter: base height
#return: area
def areaofTriangle (base, height):
return 1/2*base*height
user_base= float(input("Enter the base of the triangle: "))
user_height= float(input("Enter the height of the triangle: "))
#function call
print ("The area of the triangle is", areaofTriangle (user_base, user_height))
#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 (2,3))
print (max2 (2,99))
print (max2 (2,55))
#write a function that returns the largest of three values
# name: max3
# arguments: num1, num2, num3
#return: the largest value
|
float()
|
Change number to be decimal number.
|
|
print()
|
Show information that you want on the screen.
|
|
int()
|
Change the number/string into a integer.
|
|
str()
|
A list of number, letter and symbols.
|
|
input()
|
Gain information from user.
|
|
len()
|
The length of the string
|
|
string * number
|
Combines that string in the amount of numbers.
|
|
string *string
|
Crash!
|
|
number * number
|
Multiple (Math)
|
|
string **string
|
Crash!
|
|
number ** number
|
Exponent (Math)
|
|
string ** number
|
Crash!
|
user_number = input("What number do you want to count down?")
number = int(user_number)
countdown_string = ' '
while number > 0:
countdown_number =countdown_string + str(number) + " "
number = number -1
#print(number)
print (countdown_string)
def myprint (text): #text is (something your giving to the function) an argument (parameter) to the function
print ("***" + str(text) + "***")
return #This exits the function
myprint(1)
myprint(2.5)
myprint ("hello")
def myprint2 (text, decoration):
print (decoration + text + decoration)
return
myprint2 ("hello", "+++")
myprint2 ("hello", "_=_=_=")
myprint2 ("hello", ">>>>>>")
def double (number):
return number * 2 #return value
print(double(2))
myvar = double(double(3)) #same as double (6) because double(3) == 6
print(myvar)
while True:
user_input = input ("Enter a number: ")
user_input = int(user_input)
if user_input > 0:
print (user_input, "is positive.")
elif user_input < 0:
print (user_input, "is negative.")
elif user_input == 0:
print (user_input, "is zero.")
def areaOfCircle (r): #r=radius
pi = 3.1415
area = pi* r **2
return area
user_radius = input ("Enter the radius:")
radius = float(user_radius)
print ("The area of the circle is", areaOfCircle(radius))
mylist = [1,2,3]
for item in mylist:
print (item)
user_input = input("Enter a number:")
user_input = int (user_input)
print (user_input*5)
x = false
print (x and True or 1 ==1)
#OUTPUT = TRUE
def max3 (num1, num2, num3):
maxvalue = num1
if maxvalue < num2:
maxvalue = num2
if maxvalue < num3:
maxvalue = num3
return maxvalue
print (max3 (8,4,3))
#write a function that returns the largest number in the list
#name : maxlist
#argument: list
#return: the largest value in the list
Rules for giving names
- letters
- numbers (Can't be the first letter)
- underscore _
Valid
- _mystr
- my3
- Hello_there
Invalid name
- 3my="hi" -- cannot start with number
-firstname ="hi"
= first-name
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
while True:
word = input("Please enter a word: ")
index = 0
reverse = ''
while index < len(word):
reverse = word[index] + reverse
index = index + 1
print ("Reverse ", reverse)
#how to create a function 1
def NameofFunction (myvar1, myvar2):
print ("hello")
return myvar1, myvar2
#function call
NameofFunction (2, 3)
#Code above, prints only hello
#2
def NameofFunction (myvar1, myvar2):
print ("hello")
return myvar1 + myvar2
#function call
NameofFunction (2, 3)
myanswer = NameofFunction (4,1)
print (myanswer)
#code 2 prints out hello hello and 5
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:
print ("Error")
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)
print (calc(1,2, "sum"))
print (calc(4,2, "diff"))
print (calc(10,0, "div"))
print (calc(2,12, "product"))
#Write the function compute volume of prism
#name: volumerOfPrism
#Parameter: base, height, prism_height
#return volume
def volumeOfPrism (base, height, prism_height):
#area * prism_height
volume = areaOfTriangle (base,height)* prism_height
return volume
user_prism_height = float(input("Enter the prism height: "))
print ("The volume of the prism is", volumeOfPrism (user_base, user_height, user_prism_height))
mynum = -100
while mynum <-1:
print (mynum)
mynum= mynum + 2
def maxlist (list):
maxvalue =(list[0])
for item in list:
if maxvalue < item:
maxvalue = item
return maxvalue
print (maxlist(range(0,123)))
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.