0% found this document useful (0 votes)
17 views

Python Lec 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Python Lec 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Python Lecture 1

Basic Python programs,


defining functions
Python!
• Created in 1991 by Guido van Rossum (now at Google)
– Named for Monty Python

• Useful as a scripting language


– script: A small program meant for one-time use
– Targeted towards small to medium sized projects

• 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

• Opens a Web browser tab localhost:8888/lab


Create a new Notebook
Select Python3 Kernel
Our First Python Program

• Notebook consists of Cells- Code/Markdown


• Type in the cell (Code)
• In JupyterLab you can execute a cell by
Shift+Enter takes you to a new cell
• Ctrl+Enter remains in the same cell
Jupyter Notebook
Jupyter Notebooks
This document that you're currently reading is a "Jupyter Notebook", and you've
probably heard about it before. It's like a text document, but you can run code on
it! It can also display inline graphs, pull data from Databases or show excel
spreadsheets live!
Mildly interesting fact of the day:
Jupyter is a nod to 3 languages: Julia, Python, and R. Source
This is a really quick tutorial on how to get started with Jupyter notebooks (and
lab). It shouldn't take more than 10 minutes and you'll be writing Python code right
away.
Everything is a cell
Jupyter Notebooks are organized as a set of "cells". Each cell can contain different
types of content: like Python code (or R, Julia, etc), images or even human
readable text (markdown)
Our First Python Program
• Python does not have a main method like Java
– The program's main code is just written directly in the file
• Python statements do not end with semicolons

1 print ("Hello, world!“)


Functions
• Function: Equivalent to a static method in Java.
• Syntax:
def name():
statement 1 # Prints a helpful message.
2 def hello():
statement 3 print ("Hello, world!“)
... 4
5 # main (calls hello twice)
statement 6 hello()
7 hello()

– Must be declared above the 'main' code


– Statements inside the function must be indented
– Python uses indentation to indicate blocks, instead of {}
Variables
• Variable is the name of a memory location
• Python is weakly typed:
you don’t declare variables to be a specific type
• A variable has the type that corresponds to the value you
assign to it
• Variable names begin with a letter or an underscore and can
contain letters, numbers, and underscores
• Python has reserved words that you can’t use as variable
names

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

– Parentheses can be used to force a certain order of


evaluation.
(1 + 3) * 4 is 16

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>

• Note the colon following the else


• This works exactly the way you would expect

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

• The range function specifies a range of integers:


range(start, stop) - the integers between start
(inclusive) and stop (exclusive)

• It can also accept a third value specifying the change


between values.
range(start, stop, step) - the integers between
start (inclusive)
and stop
(exclusive) by step

25
Range

• Print the following number pattern using


Python’s range() and for loop.
1
22
333

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 |
\ /
\______/
______
/ \
/ \
+--------+

You might also like