|
| 1 | +##################################### |
| 2 | +#### Numbers and more in Python! #### |
| 3 | +##################################### |
| 4 | + |
| 5 | +# In this lecture, we will learn about numbers in Python and how to use them. |
| 6 | +# |
| 7 | +# We'll learn about the following topics: |
| 8 | +# |
| 9 | +# 1.) Types of Numbers in Python |
| 10 | +# 2.) Basic Arithmetic |
| 11 | +# 3.) Differences between Python 2 vs 3 in division |
| 12 | +# 4.) Object Assignment in Python |
| 13 | + |
| 14 | +# Types of numbers |
| 15 | +# |
| 16 | +# Python has various "types" of numbers (numeric literals). We'll mainly focus on |
| 17 | +# integers and floating point numbers. |
| 18 | +# |
| 19 | +# Integers are just whole numbers, positive or negative. For example: 2 and -2 are |
| 20 | +# examples of integers. |
| 21 | +# |
| 22 | +# Floating point numbers in Python are notable because they have a decimal point |
| 23 | +# in them, or use an exponential (e) to define the number. For example 2.0 and -2.1 |
| 24 | +# are examples of floating point numbers. 4E2 (4 times 10 to the power of 2) is |
| 25 | +# also an example of a floating point number in Python. |
| 26 | +# |
| 27 | +# Throughout this course we will be mainly working with |
| 28 | +# integers or simple float number types. |
| 29 | + |
| 30 | + |
| 31 | +# Now let's start with some basic arithmetic. |
| 32 | + |
| 33 | +# Basic Arithmetic |
| 34 | +# Addition |
| 35 | +2+1 |
| 36 | + |
| 37 | +# Subtraction |
| 38 | +2-1 |
| 39 | + |
| 40 | +# Multiplication |
| 41 | +2*2 |
| 42 | + |
| 43 | + |
| 44 | +# Division |
| 45 | +3/2 |
| 46 | + |
| 47 | +# Powers |
| 48 | +2**3 |
| 49 | + |
| 50 | +# Can also do roots this way |
| 51 | +4**0.5 |
| 52 | + |
| 53 | +# Order of Operations followed in Python |
| 54 | +2 + 10 * 10 + 3 |
| 55 | + |
| 56 | +# Can use parenthesis to specify orders |
| 57 | +(2+10) * (10+3) |
| 58 | + |
| 59 | + |
| 60 | +## Variable Assignments |
| 61 | +# |
| 62 | +# Now that we've seen how to use numbers in Python as a calculator let's see how |
| 63 | +# we can assign names and create variables. |
| 64 | +# |
| 65 | +# We use a single equals sign to assign labels to variables. |
| 66 | +# You also don't need to specify the keyword var. |
| 67 | +# Let's see a few examples of how we can do this. |
| 68 | + |
| 69 | +# Let's create an object called "a" and assign it the number 5 |
| 70 | +a = 5 |
| 71 | + |
| 72 | +# Now if I call a in my Python script, Python will treat it as the number 5. |
| 73 | + |
| 74 | +# Adding the objects |
| 75 | +a+a |
| 76 | + |
| 77 | +# What happens on reassignment? Will Python let us write it over? |
| 78 | + |
| 79 | +# Reassignment |
| 80 | +a = 10 |
| 81 | + |
| 82 | +# Check |
| 83 | +a |
| 84 | + |
| 85 | + |
| 86 | +# Yes! Python allows you to write over assigned variable names. We can also use |
| 87 | +# the variables themselves when doing the reassignment. Here is an example of what I mean: |
| 88 | + |
| 89 | +# Check |
| 90 | +a |
| 91 | + |
| 92 | +# Use A to redefine A |
| 93 | +a = a + a |
| 94 | + |
| 95 | +# Check |
| 96 | +a |
| 97 | + |
| 98 | + |
| 99 | +# The names you use when creating these labels need to follow a few rules: |
| 100 | +# |
| 101 | +# 1. Names can not start with a number. |
| 102 | +# 2. There can be no spaces in the name, use _ instead. |
| 103 | +# 3. Can't use any of these symbols :'",<>/?|\()!@#$%^&*~-+ |
| 104 | +# 3. It's considered best practice (PEP8) that the names are lowercase. |
| 105 | +# |
| 106 | +# Using variable names can be a very useful way to keep track of different |
| 107 | +# variables in Python. For example: |
| 108 | + |
| 109 | +# Use object names to keep better track of what's going on in your code! |
| 110 | +my_income = 100 |
| 111 | + |
| 112 | +tax_rate = 0.1 |
| 113 | + |
| 114 | +my_taxes = my_income*tax_rate |
| 115 | + |
| 116 | +# Show my taxes! |
| 117 | +my_taxes |
| 118 | + |
| 119 | + |
| 120 | +# So what have we learned? We learned some of the basics of numbers in Python. |
| 121 | +# We also learned how to do arithmetic and use Python as a basic calculator. |
| 122 | +# We then wrapped it up with learning about Variable Assignment in Python. |
| 123 | +# |
| 124 | +# Up next we'll learn about Strings! |
0 commit comments