basic values and types
- calculate
2+8
and put the result in tha variablex
.- print the value of
x
hint: use the function
print()
- what is the type of
x
?hint: use the function
type()
- print the value of
- calculate
5 / 2
and put the result in the variabley
. printy
and the type ofy
- put
"hello world"
into a variable namedgreeting
.- print the variable and the type of the variable
- what is the
len
ofgreeting
?hint: use the function
len()
- put the values
1
,2
and3
intolist1
and the values"a"
,"b"
and"c"
intolist2
.- calcluate
list1
+list2
and put the result intolist3
- print
list3
- print the
type
oflist3
- print the
len
oflist3
- calcluate
# 1
x = 2 + 8
print(type(x), x)
# 2
y = 5/2
print(type(y), y)
# 3
greeting = "hello world"
print(type(greeting), greeting)
print(len(greeting))
# 4
list1 = [1,2,3]
list2 = ["a", "b", "c"]
list3 = list1 + list2
print(type(list3), list3)
print(len(list3))
getting help
suppose we want to understand how to use a function. we can use the help()
function for that!
- let’s try to use the function
str.upper()
, by copying this code below and running it:print( "hello".upper() )
- lets see how to get help for a function. type the following code into a cell below. this will explain what the function
str.upper
doeshelp("hello".upper)
# 1
print( "hello".upper() )
# 2
help("hello".upper)
some simple string functions
- lets get help for all the cool functions the type
str
has. lets callhelp(str)
. run the following code:help(str)
-
read the documentation you’ve just printed, for the following functions:
lower, upper, title, islower, isupper
and try to figure out what they do. you can try to use them, read the documentation again, google for them. anything that helps you understand. - convert this string to uppercase
"the quick brown fox jumped over the lazy dog"
hint: use one of the functions you’ve learned in question #2
- is the string
"I JusT mEt YoU, anD tHIs is CraaZy, sO CAll mE mayBe"
in lower case? can you convert it to lower case?hints :
- there is a function that checks if a string is lower case
- there is a function that converts a string to lower case
# 1
help(str)
# 3
sentence = "the quick brown fox jumped over the lazy dog"
print(sentence.upper())
# 4
crazy = "I JusT mEt YoU, anD tHIs is CraaZy, sO CAll mE mayBe"
print()
print("is lower? " , crazy)
print(crazy.islower())
print()
print("is lower? ", crazy.lower())
print(crazy.lower().islower())
input from the user
run the following code to get input from the user (you are the user!)
name = input("what is your name: ")
print("hi", name)
your programs now have the power to ask questions!
name = input("what is your name: ")
print("hi", name)
BMI: Body mass index
In health the BMI (body mass index) is defined as \(BMI=\frac{weight}{height^2}\)
where the weight is in kilograms, and the height is in meters.
for example for weight 80kg and height 2.0m the BMI is $80 / (2^2) = 80 /4 = 20 $
- put 97 into a variable called
weight
- put 1.84 into a variabled called
height
- write code to compute and print the bmi
weight = 97
height = 1.84
bmi = weight / height**2
print(bmi)
BMI with input from user
- use the
input()
function to ask the user for his weight, put in variableweight
- what is the type of the variable
weight
? - use the following code to convert the value to a
float
w = float(weight)
what is the type of
w
now? - ask the user for his height and convert to float
- compute and print the bmi
weight = input("what is your weight? ")
print(type(weight), weight)
w = float(weight)
print(type(w), w)
height = input("what is your height? ")
h = float(height)
bmi = w/h**2
print(bmi)
help for str.split() function
print the help
for the function str.split
hint: use the
help()
function
help(str.split)
split a string
- create the following string, and put it in a variable called
sent
"the quick brown fox jumped over the lazy dog"
-
then split it into a list so that each word is an an element in the list.
and put the result into another variable calledwords
- print the variable
words
expected ouput:
['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
sent = "the quick brown fox jumped over the lazy dog"
words = sent.split()
print(words)
str.join()
- use the function
help
to read the documentation for the str.join function - read this code
mylist = ["one", "two", "three"] result = " ".join(mylist) print(result)
- can you guess what this code does ?
- what is the function join?
- use help or documentation online if you can’t guess
- copy this code to the cell below and run it to see if you were right
help(str.join)
# one two three
# join will add a space ' ' between each two words in mylist
mylist = ["one", "two", "three"]
result = " ".join(mylist)
print(result)
using str.upper() and str.join()
-
split the sentence
"the quick brown fox jumped over the lazy dog"
into words using the function .split() and put the result in a variable calledwords
. -
replace the the words “dog” and “fox” in the
words
list with uppercase “DOG” and “FOX”HINTS:
- use the .upper() function
- use indexing like
words[3]
to acccess a particular word in thewords
list
-
use the function join to convert the list back into a string
-
print the result
expected output:
"the quick brown FOX jumped over the lazy DOG"
words = "the quick brown fox jumped over the lazy dog".split()
# print(words)
words[3] = words[3].upper()
words[-1] = words[-1].upper()
# print(words)
result = " ".join(words)
print(result)
4 boom
in this exercise, we’re going to take some song lyrics, and replace every 4th word of each line with the word “boom”. and then print out the lyrics.
NOTE: this exercise is simple but takes a lot of lines of code. we will learn loops IN THE NEXT LESSON. loops will help us write shorter solutions, but for now YOU DONT NEED LOOPS
use the following code, which puts an important 😜 string in a variable named song
song = """
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say good bye
Never gonna tell a lie and hurt you
"""
- split this string into a list of lines. put it in variable called
lines
hint: string have a function called
.splitlines()
- split the line 0 into words. put it in variable called words0
- now also split line 1. put it into
words1
- repeat for line 2 …, line 5, put each into word1, … word5
hint: this should take you 6 simples lines.
- now also split line 1. put it into
- replace the 4th word of line 1, line 2, … line 6 with the word
"boom"
`hint: you don’t need loops. this again takes 6 lines
- now join
words0
. put the result into variable boom0- continue to join
words1
,words2
…words5
intoboom1
,boom2
…boom5
- continue to join
-
create a list that contains
boom0
,boom1
, …boom5
, call itboom
- join the lines in
boom
back into a string. put it into a variable calledresult
hint: use this code
result = "\n".join(boom)
- print the result
expected output:
Never gonna give you boom Never gonna let you boom Never gonna run around boom desert you Never gonna make you boom Never gonna say good boom Never gonna tell a boom and hurt you
### useful: data
song = """Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say good bye
Never gonna tell a lie and hurt you
"""
lines = song.splitlines()
words0 = lines[0].split()
words1 = lines[1].split()
words2 = lines[2].split()
words3 = lines[3].split()
words4 = lines[4].split()
words5 = lines[5].split()
words0[4] = "boom"
words1[4] = "boom"
words2[4] = "boom"
words3[4] = "boom"
words4[4] = "boom"
words5[4] = "boom"
boom0 = " ".join(words0)
boom1 = " ".join(words1)
boom2 = " ".join(words2)
boom3 = " ".join(words3)
boom4 = " ".join(words4)
boom5 = " ".join(words5)
boom = [boom0, boom1, boom2, boom3, boom4, boom5]
result = "\n".join(boom)
print(result)
Dictionary
-
create a dictionary that maps the name of cheeses to prices of 100mg of that cheese.
gouda costs 4.99 Edam costs 2.45 Camambert costs 7.75 Bree costs 7.27
- without using “head math”, write simple code to compute the costs of gouda, edam and camambert together
hint: the solution is one simple but long line of code, which uses the
+
operator 3 times - add a new cheese to the dictionary:
Cottage
cheese, which costs3.2
hint: the solution takes just one very simple line of code
- the government has added a new tax on milk products, now every cheese costs has increased by 27% ! can you update the costs in the dictionary?
hint: the solution takes 5 (almost identical) lines of code
- can you use the
round()
function to round the prices of cheese, so that it has costs in dollars and cents (Shekels and agorot)?hint: what does the following code do?
pi = 3.14159265 print(pi) pi_rounded = round(pi, 2) print(pi_rounded)
# 1
print()
print(1)
print('-' * 10)
costs = {
"Gouda" : 4.99 ,
"Edam" : 2.45,
"Camambert" : 7.75,
"Bree" : 7.27
}
print(costs)
# 2
print()
print(2)
print('-' * 10)
print(costs["Gouda"] + costs["Edam"] + costs["Camambert"])
# 3
print()
print(3)
print('-' * 10)
costs["Cottage"] = 3.2
print(costs)
# 4
print()
print(4)
print('-' * 10)
costs['Gouda'] = costs['Gouda'] * 1.27
costs['Edam'] = costs['Edam'] * 1.27
costs['Camambert'] = costs['Camambert'] * 1.27
costs['Bree'] = costs['Bree'] * 1.27
costs['Cottage'] = costs['Cottage'] * 1.27
print(costs)
# 5
print()
print(5)
print('-' * 10)
pi = 3.14159265
print(pi)
pi_rounded = round(pi, 2)
print(pi_rounded)
costs['Gouda'] = round(costs['Gouda'], 2)
costs['Edam'] = round(costs['Edam'], 2)
costs['Camambert'] = round(costs['Camambert'], 2)
costs['Bree'] = round(costs['Bree'], 2)
costs['Cottage'] = round(costs['Cottage'], 2)
print(costs)
len and indexing
quote = "everything should be as simple as possible, but not simpler. Albert Einstein"
- write code to check how many letters/characters are in this qoute.
hint: use the function len()
-
split this qoute into words. how many words are in this quote?
- write code to print:
- the 1st letter in the quote
- the 7th word
- the 1st letter of the 7th word
- the last letter in the quote
- the last letter of the 9th word
hint: the function
len()
is useful to figuring out what is the last letter/word
# 1
quote = "everything should be as simple as possible but not simpler. Albert Einstein"
print(len(quote))
# 2
words = quote.split()
print(len(words))
# 3
print(quote[0])
print(words[6]) # 7th word
print(words[6][0]) # 1st letter of 7th word
print(quote[len(quote) - 1]) #last letter
word8 = words[8] # 9th word
print(word8[len(word8) -1]) #last letter of 9th word
len, indexing and input
- what would the following code print? first try to answer yourself, then copy the code and run it to see if you guessed right
hi = "hello "
greeting = hi * 3
print(greeting)
-
use the
input()
function to ask the user for full name. -
print the length of the name
-
print the last letter of the name 100 times
# 1
hi = "hello "
greeting = hi * 3
print(greeting)
name = input("what is your full name? ")
print(len(name))
print(name[len(name) -1] * 100)