|
==
|
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
|
|
single quoted
|
'example'
|
|
double quoted
|
"example"
|
|
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
|
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 )
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)
def bacon():
print("hello it's bacon")
print("line 2")
print("line 3")
print("line 4")
return
bacon()
bacon()
bacon()
def myprintnew (text, decoration):
print(decoration + str(text) + decoration)
return
myprintnew(1, "+++")
myprintnew('hello','-=-=-=-=-=-=-=-=')
myprintnew(1, "@@@@@@@")
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) )
|
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
|
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
def myprint(text):
print("***" + str(text) + "***")
return
myprint(1)
myprint("hello")
myprint(2.5)
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))
_var1 = 1
_var1 = 3
_var1 + 100
print(_var1)
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))
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))
|
string * number
|
combine that string multiple times
|
|
string * string
|
crash
|
|
number * number
|
math - multiply
|
|
string ** string
|
crash
|
|
number ** number
|
math - multiply
|
|
string ** number
|
crash
|
|
string + string
|
combine together
|
|
string + number
|
crash
|
|
number + number
|
math-addition
|
|
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)
|
mystr = "hello THERE"
print (mystr.upper())
print (mystr.lower())
print (mystr.capitalize())
print (mystr.title())
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)
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)
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) )
def doubleIt(number):
return number*2
print (doubleIt(3))
print (doubleIt(doubleIt(4)))
myvar = 12
myvar= doubleIt(myvar)
myvar= doubleIt(myvar)
print(myvar)
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.