Lecture 2
Printing, Strings, and Constants
Benjamin Dicken
CSc 250 - Spring 2018
Programming in Python
Wing 101
Wing 101
Print!
print("Text to print in here")
Print!
Print!
print("Don’t know much about history")
print("Don’t know much biology")
print("Don’t know much about history Don’t know much biology")
Printing some lyrics
If I ever took a loss, I learned a lesson
I won't ever think I'm better than the next man
I've been down before the come up, I ain't stressin'
Baby I'm too busy countin' all these blessings
(From “Blessings” by Lecrae)
ICA
Printing some lyrics
Solution:
print("If I ever took a loss, I learned a lesson")
print("I won't ever think I'm better than the next man ")
print("I've been down before the come up, I ain't stressin'")
print("Baby I'm too busy countin' all these blessings")
ICA
Print!
Around the world, around the world
Around the world, around the world
Around the world, around the world
Around the world, around the world
(x18)
Print!
print("Around the world, around the world")
print("Around the world, around the world")
print("Around the world, around the world")
print("Around the world, around the world")
print("(x18)")
Use a variable
lyric = "Around the world, around the world"
print(lyric)
print(lyric)
print(lyric)
print(lyric)
print("(x18)")
Printing repetitive lyrics
Yeah, they like the way I do this
When I crank it like a chainsaw
Yeah, they like the way I do this
When I crank it like a chainsaw
(From “Chainsaw” by Family Force 5)
ICA
Printing repetitive lyrics
Solution:
yeah = "Yeah, they like the way I do this"
chain = "When I crank it like a chainsaw"
print(yeah)
print(chain)
print(yeah)
print(chain)
ICA
Use a variable
Right about now The funk soul brother
Check it out now The funk soul brother
Right about now The funk soul brother
Check it out now The funk soul brother
Right about now, 'bout now 'Bout now, 'bout now
Use a variable
Right about now The funk soul brother
Check it out now The funk soul brother
Right about now The funk soul brother
Check it out now The funk soul brother
Right about now, 'bout now 'Bout now, 'bout now
Use a variable
Right about now The funk soul brother
Check it out now The funk soul brother
Right about now The funk soul brother
Check it out now The funk soul brother
Right about now, 'bout now 'Bout now, 'bout now
Use a variable
Right about now The funk soul brother
Check it out now The funk soul brother
Right about now The funk soul brother
Check it out now The funk soul brother
Right about now, 'bout now 'Bout now, 'bout now
Use a variable
right = "Right about now"
check = "Check it out now"
funk = " The funk soul brother"
Bout = ", 'bout now 'Bout now, 'bout now"
Use a variable
right = "Right about now"
check = "Check it out now"
funk = " The funk soul brother"
Bout = ", 'bout now 'Bout now, 'bout now"
Now how do we use them?
Use a variable
right = "Right about now"
check = "Check it out now"
funk = " The funk soul brother"
bout = ", 'bout now 'Bout now, 'bout now"
print(right + funk)
print(check + funk)
print(right + funk)
print(check + funk)
print(right + bout)
String concatenation
String concatenation
right = "Right about now"
check = "Check it out now"
funk = " The funk soul brother"
bout = ", 'bout now 'Bout now, 'bout now"
another1 = right + right
another2 = funk + another1
ICA
More Strings
lyric = "Around the world, around the world"
print(lyric)
print(lyric)
print(lyric)
print(lyric)
print("(x18)")
Multiplication
class = "CSC 250 - Essential Computing for the Sciences"
print(class * 5)
Multiplication
class = "CSC 250 - Essential Computing for the Sciences"
print(class * 5)
name = "Your name here! " * 3
print(name * 2)
More Strings
lyric = "Around the world, around the world"
print(lyric)
print(lyric)
print(lyric)
print(lyric)
print("(x18)")
More Strings
lyric = "Around the world, around the world"
print(lyric)
print(lyric)
print(lyric)
print(lyric)
print("(x18)")
lyric = "Around the world, around the world"
print(lyric * 4)
print("(x18)")
Newline
Printing with newlines
AAA
BBBBBB
CCCCCCCCC
DDDDDD
EEE
ICA
Printing with newlines
Solution:
print("AAA\nBBBBBB\nCCCCCCCCC\nDDDDDD\nEEE")
ICA
More Strings
lyric = "Around the world, around the world\n"
print(lyric * 4 + "(x18)")
Which is better? Why?
print("Around the world, around the world")
print("Around the world, around the world")
print("Around the world, around the world")
print("Around the world, around the world")
print("(x18)")
lyric = "Around the world, around the world\n"
print(lyric * 4 + "(x18)")
Constant numbers
REPEAT = 4
lyric = "Around the world, around the world\n"
print(lyric * REPEAT + "(x18)")
Dynamic Sizing
WIDTH = 0
/ \
<-O->� \ /
WIDTH = 5
/ \
<------O------>� \ /
WIDTH = 3
/ \
<----O---->� \ /
ICA