Python Fundamentals

*Note: This lesson was written using Python 2.xx. If you are using Python 3.xxx any changes to the code will be annotated under headings: Python 3.xxx

Open up a new Jupyter Notebook. Remember, when working with Jupyter Notebooks, you need to hit Shift+Enter to execute your code.

pythonInstall4

Arithmetic Operators

Python Arithmetic Operators are pretty standard. Take note of the division problem in line [9]. 10/7 is not 1, but by default Python only shows integer answers

pythonFundamentals1

You can work around it by defining the first number as a float()

pythonFundamentals2

Python 3.xxx

Python 3 handles division differently.  To get integer numbers only in division, use //.

10//7
1
10/7
1.4285714285714286

Arithmetic Operators

  • + : Addition
  • – : Subtraction
  • * : Multiplication
  • / : Division
  • % : Modulus (returns the remainder of a division problem: 5%2=1)
  • ** : Exponent (4**2 = 16)

Variables

Variables in Python are pretty straight forward. Unlike other programming languages, you do not need to define the variables first. Python dynamically assigns the data type.

Three main rules:

  • Variables must start with a letter or _
  • Variables are case sensitive
  • Avoid using command keywords (print, def, for)

pythonFundamentals3

notice lowercase ‘a‘ returns an error

Remember, Jupyter notebooks only return the last command. If you want both variables, use the print command

pythonfundaments4

Python 3.xxx

In Python 3, print statements require ().

print (A)
print (B)

You can perform arithmetic functions on variables

pythonfundaments5

Python 3.xxx

print (C)

And of course, variables can hold strings as well as numbers

pythonfundaments7

Python 3.xxx

print (E)
print (F)

 

One thought on “Python Fundamentals

Please Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s