Python Lec 1
Python Lec 1
• Used by:
– Google, Yahoo!, Youtube
– Many Linux distributions
– Games and apps (e.g. Eve Online)
Installing Python
Download Anaconda: MIT OCW Python:
• https://ocw.mit.edu/courses/electrical-
https://www.anaconda.com/product engineering-and-computer-science/6-189-a-
s/individual#download-section gentle-introduction-to-programming-using-
python-january-iap-2011/lectures/
JupyterLab and Notebooks
• Has all the Python 3 and Data • https://github.com/jupyterlab/jupyterlab
• https://jupyterlab.readthedocs.io/en/stable/use
analysis packages we need r/notebook.html
• Has JupyerLab which we will be • https://jupyter.brynmawr.edu/services/public/d
blank/Jupyter%20Notebook%20Users%20Man
using for our Lectures and Labs ual.ipynb
• Launch JupyterLab
12
Variables
• Do the following:
x=5
print(type(x))
x=“this is text”
print(type(x))
x=5.0
print(type(x))
13
Printing
• You’ve already seen the print statement
• You can also print numbers with formatting
• These are identical to Java or C format specifiers
14
Comments
• All code must contain comments that describe what it does
• In Python, lines beginning with a # sign are comment lines
You can also have comments on the same line as a statement
# This entire line is a comment
x=5 # Set up loop counter
15
Operators
• Arithmetic operators we will use:
– + - * / addition, subtraction/negation, multiplication,
division
– % modulus, a.k.a. remainder
– ** exponentiation
• precedence: Order in which operations are computed.
– * / % ** have a higher precedence than + -
1 + 3 * 4 is 13
16
Expressions
• When integers and reals are mixed, the result is a real
number.
– Example: 1 / 2.0 is 0.5
– The conversion occurs on a per-operator basis.
– 7 / 3 * 1.2 + 3 / 2
– 2 * 1.2 + 3 / 2
– 2.4 + 3 / 2
– 2.4 + 1
– 3.4
17
Math Functions
# Python requires us to explicitly load the libraries
– import math
– from math import pi
– #We can also change the name of the library
18
Relational Operators
• Many logical expressions use relational operators:
19
Logical Operators
• These operators return true or false
20
The if Statement
• Syntax:
if <condition>:
<statements>
x = 5
if x > 4:
print(“x is greater than 4”)
print(“This is not in the scope of the if”)
21
The if Statement
• The colon is required for the if
• Note that all statement indented one level in from the if
are within it scope:
x = 5
if x > 4:
print(“x is greater than 4”)
print(“This is also in the scope of the if”)
22
The if/else Statement
if <condition>:
<statements>
else:
<statements>
23
The for Loop
• This is similar to what you’re used to from C or Java, but not the
same
• Syntax:
for variableName in groupOfValues:
<statements>
• variableName gives a name to each value, so you can refer to it
in the statements.
• groupOfValues can be a range of integers, specified with the
range function.
• Example:
for x in range(1, 6):
print (x, "squared is", x * x)
24
Range
25
Range
26
The while Loop
• Executes a group of statements as long as a condition is
True.
• Syntax:
while <condition>:
<statements>
• Example:
number = 1
while number < 200:
print number,
number = number * 2
27
Exercise
• Write a Python program to compute and display the first 16
powers of 2, starting with 1
28
Exercise
• Write a program to get an output:
______
/ \
/ \
\ /
\______/
\ /
\______/
+--------+
______
/ \
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+