Vocabulary
Varriable |
holds a value and can be change |
String |
a list of characters such as number, letters, symbol |
Integer number |
whole number/counting number |
Floating number |
the number in decimal |
Syntax |
grammar/structure of language |
Modulo |
find the remainder |
Boolean |
true/false |
Conditional
if........ |
If the statement is true then do |
then..... |
command under then else do |
else..... |
command under else |
while..... |
While this is true loop the command under the conditional |
Naming Conventions
Rules for naming variable: |
- letters |
- numbers |
- can start with letters or underscores ONLY |
-underscores (_) |
- 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 |
palindrome
reverse = ""
letter_num = 0
word = input('type in a word: ')
while letter_num < len(word):
reverse = word[letter_num] + reverse
letter_num = letter_num + 1
if reverse == word:
print ('it is palindrome')
else:
print ('it is not palindrome')
|
maxvalue
def max2(num1,num2):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
return maxvalue
print (max2(4,5))
print (max2(33,5))
def max3(num1,num2,num3):
maxvalue = num1
if num2 > maxvalue:
maxvalue = num2
if num3 > maxvalue:
maxvalue = num3
return maxvalue
print (max3(1,2,3))
|
|
|
Function
print() |
show information that you want on the screen |
int() |
change number to be integer |
float() |
change the number to decimal number |
input() |
gain information from user |
str() |
a list of number, letter and symbol |
len() |
the length of the string |
# |
comment, no effect |
Math
== |
equal to |
!= |
not equal to |
< |
less than |
> |
more than |
<= |
less than or equal to |
>= |
more than or equal to |
% |
Modulo, Find the remainder |
+ |
add |
- |
subtract |
* |
multiply |
/ |
divide |
** |
exponent |
Function
def printDefinitions():
if word == "variable":
print("""variable is value that you can change""")
elif word == "function"
print("""function is is define a box of code""")
elif word == "parameter"
print("""parameter is a valuve of number that you give to the function""")
elif word == "argument"
print("""argument is set of somethings that you give to the function""")
elif word == "function call"
print("""function call is when you call the function and it will run""")
elif word == "string"
print("""string is anything you can put""")
else:
print("unknown word")
return
while True:
user_input = input("Enter word:")
printDefinition(user_input)
|
maxlist
def maxlist(list):
maxvalue = list[0]
for item in list:
if item > maxvalue:
maxvalue = item
return maxvalue
mylist = [1,2,3,4,55,66,777,0,1]
print(maxlist(mylist))
|
exercise 2 odd
#Recieves input from the user, converts it to an integer and prints the product of the integer
user_number = int(input("Enter a number"))
print(user_number*5)
#prints all the even numbers from -100 to -1 using while loop
user_number = -100
while user_number< -1:
print(user_number)
user_number = user_number +2
#print all the item using a loop
mylist = ['cokezero', 'bacon', 'pesi']
for item in mylist:
print(item)
#areaOfEllipse()
def areaOdEllipse(radius1, radius2):
pi = 3.145
area = pi radius1 radius2
return area
#function call
area1 = areaOfEllipse(2,3)
print(area1)
#The user enters a negative interger, exit the loop and print how many of the num enter were even and odd
evencount = 0
oddcount = 0
While True:
num = int(input("Enter a positive integer"))
if num < 0:
print("Enter numbers:", evenCount)
print("Odd numbers:", oddCount)
break
else:
if(num % 2) == 0
evenCount = evencount + 1
else:
oddCount = oddCount + 1
|
|
|
Additional
string + string |
squishes them together |
string + number |
crash |
number + number |
math(addition) |
Multiplication & Exponents
string * string |
CRASH!!! |
string * |
combines the strings |
number |
multiple time |
number * number |
math (multiply |
string ** number |
math (multiply)CRASH!!! |
number ** number |
exponent (Math) |
string ** number |
CRASH!!! |
Import Random
import random
intlist = [5,10,15,20,25]
random_int = random.choice(intlist)
print (random_int)
fplist = [1.1,2.2,3.3,4.4,5.5]
random_fp = random.choice(fplist)
print (random_fp)
strlist = ['rat','cat','fat','mat','sat']
random_str = random.choice(strlist)
print (random_str)
mylist = ['blue', 'green', 'yellow', 'red', 'pink']
random_item = random.choice(mylist)
print(random_item)
myvarl = 1
myvar2 = 2
myvar3 = 3
varlist = [myvar1, myvar2, myvar3]
random_var = random.choice(varlist)
print (varlist, random_var)
|
def areaofcircle
def areaofcircle(r):
if r <= 0:
return"Error: invalid radius'
pi = 3.1415
area = pi r * 2
return area #return the area of the circle
user_radius = float (input("Enter the radius: "))
print('The area of the circle is', areaofcircle(user_radius))
|
areaoftriangle
def areaofcircle(radius):
if radius <= 0:
return "Error: invalid raadius"
pi = 3.1415
area = pi (radius*2)
return area
user_radius = float(input("Enter the radius: "))
print('The area of the circle is', areaofcircle(user_radius))
Enter the radius: 2
The area of the circle is 12.566
Enter the radius: 0
The area of the circle is Error: invalid raadius
|
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment