List code
import random
inlist = [1, 2, 3, 4, 5]
random_int = random.choice(inlist)
print(inlist, random_int)
fplist = [1.1, 1.2, 1.3, 1.4, 5]
random_fp = random.choice(fplist)
print(fplist, random_fp)
strlist = ["tien", "love", "opal"]
random_str = random.choice(strlist)
print(strlist, random_str)
mylist = ["opal", 1, 1.2]
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)
[1, 2, 3, 4, 5] 1
[1.1, 1.2, 1.3, 1.4, 5] 5
['tien', 'love', 'opal'] tien
['opal', 1, 1.2] 1.2
[1, 2, 3] 3
|
palindrome
def isPalindrome(word):
reverse = ""
letter_num=0
while letter_num<len(user_input):
reverse = user_input[letter_num]+reverse
letter_num = letter_num+1
if reverse==word:
return True
else:
return False
while True :
user_input = input("Enter a word")
if user_input == "quit":
break
isPal = isPalindrome(user_input)
if isPal == True:
print (user_input,'is parindorm')
else:
print (user_input,'is not parindorm')
break
|
Enter a word113311
113311 is parindorm
Enter a word123
123 is not parindorm
Enter a wordquit
>>>
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))
|
Basic function
def myprint(text):
print ("***" + str(text) + "***")
return
myprint("opal")
hello it's bacon
***opal***
def myprintnew(text, decoration):
print (decoration + str(text) + decoration)
return
myprintnew("opal", "m")
hello it's bacon
***opal***
mopalm
def doubleit(number):
return number * 2
print (doubleit(3))
print (doubleit(doubleit(4)))
hello it's bacon
***opal***
mopalm
6
16
|
Addition
string + string |
combine together |
string + number |
crash |
number + number |
math-addition |
Naming Convention
Rule for giving name
-letter
-numbers
-underscore_
Valid name
- _myStr
- my3
- Hello_there
Invalid name
- 3my="hi" -- cannot start with number
- first name = "hi"
- first-name
- first+name
|
Math
== |
equal to |
!= |
no equal to |
< |
less than |
> |
more than |
<= |
less than or equal to |
/ |
divide and quotient is float |
// |
divide and quotient is integer |
** |
exponent |
% |
modulo: remainder |
|
|
Short word per line
mystr = "Hello"
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1
H
e
l
l
o
|
Sample 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 )
|
Guessing game code
print("'-'-'-'-'-'-'-'-'-'-'-'")
print("Guessing Game")
print("'-'-'-'-'-'-'-'-'-'-'-'")
import random
mylist = ['opal', 'game', 'pin', 'timmy', 'tupkung']
random_item = random.choice(mylist)
print("Words :",mylist)
chance = 5
score = 0
while chance > 0:
user_guess = input("Guess a word: ")
if user_guess == random_item:
print("That's correct!")
score = score+100
print ("score: ",score)
print ("chance remaining: ",chance)
random_item = random.choice(mylist)
else:
chance = chance-1
if user_guess in mylist:
print("Sorry, wrong choice!")
else:
print("Sorry, That is not even in the list")
print("chance: ",chance)
print ("Game over! The word was", random_item)
print ("Final score is :", score)
|
Multiplication and Exponent
string * number |
combine that string |
string * string |
crash |
number * number |
Multiply (Math) |
string ** string |
CRASH!! |
number ** number |
Exponent ( Math) |
String ** number |
CRASH!! |
reverse
reverse = ""
letter_num = 0
while True==True : #loop will go forever
user_input = input('type in a string: ')
while letter_num < len(user_input):
reverse = user_input[letter_num] + reverse
letter_num = letter_num + 1
if user_input == reverse:
print("user_input is the parindorm")
else:
print("user_input is not a parindorm")
|
type in a string: 235
user_input is not a parindorm
type in a string: 12021
user_input is not a parindorm
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))
|
Sample code
mystr = "hellp THERE"
print (mystr.upper()) -all letters will become big HELP THREE
print (mystr.lower()) -all letters will become small help three
print (mystr.capitalize()) -First letter of first word will become big Help three
print (mystr.title())- first letter of each words will become big Help Three
|
|
|
Convert to binary
user_number = ' '
while user_name != ' '
user_number = input("Enter a number to convert to binary")
number = int(user_number)
binary_string = ' '
while (number > 0)
remainder = number%2
binary_string = str(remainder)+binary_string
number = number//2
print ("Binary string is", binary_string)
|
Definition
String |
list of letters/symbol/numbers |
Variable |
value that can be changed |
Integer |
whole number |
Floating Point |
decimal |
Syntax |
grammar/structure |
Boolean |
True/False |
Function
print() |
Show information that you want on the screen |
int() |
change the number to be number integer |
float() |
change the number to be number decimal |
input() |
Gain information from user |
str() |
A list of letter, number and symbols |
len() |
The length of the string |
# |
comment, no effect |
circle area
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
|
simple function
def printdefinitions(word):
if word == ("variable"):
print ("""A variable is the value that can change""")
elif word == ("function"):
print ("""A function is the blog of code that can be reused""")
elif word == ("parameter"):
print ("""A parameter is something given to the function""")
elif word == ("agrument"):
print ("""An agrument is something given to the function""")
elif word == ("string"):
print ("""A string is a lsit of characters""")
elif word == ("function call"):
print ("""A function call makes your function run""")
else:
print ("Unknown word")
return
while True: #keep the loop go forever
user_input = input("Enter word: ")
printdefinitions(user_input)
|
Enter word: hello
Unknown word
Enter word: function
A function is the blog of code that can be reused
Enter word: variable
A variable is the value that can change
Enter word:
area/volume of triangle
def areaofTriangle(b,h):
return 0.5 b h
user_base = float(input('Enter the base of triangle: '))
user_height = float(input('Enter the height of the trianglr: '))
print('The area of triangle is', areaofTriangle(user_base, user_height))
def volumeofprism(b,h,l):
volume = areaofTriangle(b,h) * l
return(volume)
user_base = float(input('Enter the base of triangle: '))
user_height = float(input('Enter the height of the trianglr: '))
user_length = float(input('Enter the length of the triangle: '))
print('The volume of prism is', volumeofprism(user_base, user_height, user_length))
|
Enter the base of triangle: 6
Enter the height of the trianglr: 6
The area of triangle is 18.0
Enter the base of triangle: 6
Enter the height of the trianglr: 6
Enter the length of the triangle: 6
The volume of prism is 108.0
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment