Basic Programming: CS 111: Computer Science For Scientists
Basic Programming: CS 111: Computer Science For Scientists
x = 45 #integer
z = -3410 #integer
myPie = 3.14 #float
wordsTogether = "Hi there" #string
wordsTogether2 = We are learning Python!" #string
isItRight = True #Boolean
isItWrong = False #Boolean
sadSauce = 25.0 #float
happySauce = 25 #int (no .)
awesomeSauce = "25" #String (quotes)
sillySauce = "True" #String (quotes)
Assignment
A note about assignment.
The right value is always
assigned to the left
x = 45
variable
This allows you to assign
the value of a variable to
another variable.
x = 25 #x has value of 25
y = 33 #y has value of 33
x = y #x has value of 33, y is unchanged
Why bother differentiating float and int?
Even though float and int both represent numbers, we
need to have two different types for these values because of
how they are stored on the computer.
int has no decimal point
float can represent more numbers.
For example,
When you reference pages in a book you use integers
Go to page 45
However, float can express more numbers
1.4234325234
Floats
Essentially floats store a limited number of digits.
So, 2/3 is stored as 0.666666666 not 0.666666666
This means that we cant tell the difference between
0.6666666666 and 0.6666666667. Since its one more digit
than we stored.
This issue is compounded by the fact that how computers
actually store float is crazy.
x = int(25.0) # x is an int
y = int(25.4) # y is 25
z = float(25) #z is a float
a = str(25) #a is the string "25"
q = int(25.4) + 3 # q = 28
w = "q is :"+str(q) # w equals "q is :28"
Input and Conversion
Recall, input() always returns a string.
So, suppose wrote
z = input("Enter an integer")
value = z + 10
print(value)
A and B A or B not B
Combing Boolean Operations
Lets go ahead and combine some Boolean operations!
Components revisited
Execution Operations
Sequential Operations
Condition Operations
Iterative Operations
Conditions allow you to
Data Operations
Variables & Assignments chose which code is
Mathematical Expressions executed
Output & Input
if
The heart of conditional operations
is the if statement
The if statement evaluates a val = int(input("enter value"))
Boolean expression and will run a if val < 10:
print("The value is < 10")
segment of code if that Boolean print("Another thing")
expression is True. print("Conditional")
val = -3
The segment of conditional code is print("always runs")
designated via indention. print(val)
count = 0
while count < 10:
print("The count is"+str(count))
print("Whatever code you want")
count+=1
print("we're done")
Components of a while loop
Generally all while loops have the same four components
count = 0
while count < 10:
print("The count is"+str(count))
print("Whatever code you want")
count+=1
print("we're done")
count = 0
while count < 10:
print("The count is"+str(count))
print("Whatever code you want")
count+=1
print("we're done")
count = 0
while count < 10:
print("The count is"+str(count))
print("Whatever code you want")
count+=1
print("we're done")
count = 0
while count < 10:
print("The count is"+str(count))
print("Whatever code you want")
count+=1
print("we're done")