ds4ad

Introduction to Programming Using Python

Follow along by typing the coding examples in your notebook.

Math in Python

Math in Python looks a lot like math you type into a calculator. Python makes a great calculator if you need to crunch some numbers and don’t have a good calculator handy.

Addition

2 + 2 
1.5 + 2.25

Subtraction

4 - 2
100 - .5
0 - 2

Multiplication

2 * 3  

Division

1 / 2
4 / 2

When you run these last two you will see that the first one is a fraction (0.5), but that even though the second is a whole number (2.0), it still gets a decimal point. This is because Python wants to make sure that when you divide two numbers (even whole numbers), you can represent the fractional part of this division.

In Python, we call the first kind of number (whole numbers with no decimal point) an integer (or int) and the second kind (numbers with decimal points, whether they are whole of not) a float.

Variables- Storing values/information

Variables allow us to give a name to something in our code. This is done using the equal sign (=). For example, if we want to say that the number 4 should be stored in the name x, we can run the following:

x = 4
y = 10

We call this “variable assignment”, because we assign a particular value to the name x.

Output

Notice how if you type 4 or 10 and hit enter, the Python interpreter spits a 4 back out:

4

But not when you assign it to a variable.

You can reassign variables as you please:

x = 4
x = 10
print(x)

Using the print function to display objects.

print(x) #we can print individual variables
print(x,y) # we can print a combination
print("x is a number",x,"y is a number",y ) # we can add comments as strings to make things more meaningful.

Variables can be used in calculations.

Order of operations works pretty much like how you learned in school. If you’re unsure of an ordering, you can add parentheses like on a calculator:

x = 3 
y = 4
print(x * y)
print(x * x)
print(2 * x - 1 * y)
print((2 * x) - (1 * y))

the spacing doesn’t matter, so this:

x = 4
x=4

Valid variable names

Variables can’t have spaces or other special characters, and they need to start with a letter. Here are some valid variable names:

magic_numbers = 1500
amountOfFlour = .75
my_name = "Jessica"

What will happen if you violate the name rule?

my weight lbs = 210

Exercises 1.0

What values do the variables weight_lbs and age have after each statement in the following program? Test your answer by executing the commands.

weight_lbs = 210
age = 35
weight_lbs = weight_lbs * 2.0
age = age - 15
print(weight_lbs)
print(age)

Strings

So far we’ve seen mostly two data types: int and floats. Another useful data type is a string, which is just what Python calls a bunch of characters (like numbers, letters, whitespace, and punctuation) put together. Strings are indicated by being surrounded by quotes:

"Hello"
"Python, I'm your #1 fan!"

You can use the function type to check the data types:

type("Hello")
type(1)
type("1")

What does the following program print out?

first, second = 'Grace', 'Hopper'
third, fourth = second, first
print(third, fourth)

One fun thing about strings in Python is that you can multiply them. Try it!

print("A" * 40)
print("ABC" * 12)
h = "Happy"
b = "Birthday"
print((h + b) * 10)

Use an index to get a single character from a string.

Problem 1.

total = 1.5 - 1/2
total
type(total)

Problem 2.

a = "quick"
b =  "brown"
c = "fox jumps over the lazy dog"
print("The " +  a * 3 + " " +  b * 3 + " " + c)