•Introductory concepts
•Installing Python
•Installing Python IDEs
•Your first Python Program
Introduction to Python
1
Introduction to Python
What is Python programming language?
2
Users vs. Programmers
• Users see computers as a set of tools - word processor,
spreadsheet, map, to-do list, etc.
• Programmers learn the computer “ways” and the computer
language
• Programmers have some tools that allow them to build new
tools
• Programmers sometimes write tools for lots of users and
sometimes programmers write little “helpers” for
themselves to automate a task
3
Why be a Programmer?
• To get some task done - we are the user and programmer
 Clean up survey data; making numerical analysis
• To produce something for others to use - a programming
job
 Fix a performance problem in the Sakai software
 Add a guestbook to a web site
4
What is Code? Software? A Program?
• A sequence of stored instructions
 It is a little piece of our intelligence in the computer
 We figure something out and then we encode it and then give
it to someone else to save them the time and energy of
figuring it out
• A piece of creative art - particularly when we do a good
job on user experience
5
Programs for Humans
•While music is playing:
• Left hand out and up
• Right hand out and up
• Flip Left hand
• Flip Right hand
• Left hand to right shoulder
• Right hand to left shoulder
• Left hand to back of head
• Right ham to back of head
• Left hand to right hit
• Right hand to left hit
• Left hand on left bottom
• Right hand on right bottom
• Wiggle
• Wiggle
• Jump
https://www.youtube.com/watch?v=XiBYM6g8Tck
6
Python!
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)
7
Python is used everywhere!
8
Interpreted Languages
• Interpreted
– Not compiled like Java
– Code is written and then directly executed by an interpreter
– Type commands into interpreter and see immediate results
Computer
Runtime
Environment
Compiler
Code
Java:
Computer
Interpreter
Code
Python:
9
Installing Python
Windows:
• Download Python from
http://www.python.org
• Install Python.
• Run Idle from the Start Menu.
Mac OS X:
• Python is already installed.
• Open a terminal and run python
or run Idle from Finder.
Linux:
• Chances are you already have
Python installed. To check, run
python from the terminal.
• If not installed, use the following.
$ sudo apt-get install software-
properties-common
$ sudo add-apt-repository
ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.8
10
The Python Interpreter
The Python Interpreter
• Allows you to type commands one-at-a-time and see results
• A great way to explore Python's syntax
– Repeat previous command: Alt+P
11
How to run Python Windows
• Run IDLE to use the interpret
• Open a new window in IDLE to write and save programs
12
How to run Python Unix
• Start the interactive shell with python
• Run a program with python /path/to/program.py
13
Python Integrated Development
Environments (IDEs)
 There are lots of IDEs for Python programming.
14
Installing Spyder
 OnWindows,
 InstallWinPython scientific Python distribution.
 It contains Spyder in this specific distribution.
 On Ubuntu/Linux, run the following command.
$sudo apt-get install spyder3
15
Your First Python Program
 With Python installed, we can write and run our first Python
program.
 Open a text editor software (like notepad) and write the
following line of code into the file.
print("Hello, World!")
 Save the file as “filename.py” in your preferred folder or
directory.
 OpenWindows or Linux terminal and change current
directory to the folder you stored your Python file.
 Then run Python filename.py command.
16
Elements of Python
 Vocabulary /Words -Variables and Reserved words
 Sentence structure - valid syntax patterns
 Story structure - constructing a program for a purpose
17
Reserved Words
You cannot use reserved words as variable names /
identifiers
False class return is finally
None if for lambda continue
True def from while nonlocal
and del global not with
as elif try or yield
assert else import pass
break except in raise
18
Interactive versus Script
 Interactive
 You type directly to Python one line at a time and it responds
 Script
 You enter a sequence of statements (lines) into a file using a text
editor and tell Python to execute the statements in the file
19
Program Steps or Program Flow
 Like a recipe or installation instructions, a program is a
sequence of steps to be done in order.
 Some steps are conditional - they may be skipped.
 Sometimes a step or group of steps is to be repeated.
 Sometimes we store a set of steps to be used over and over as
needed several places throughout the program.
20
Sequential Steps
Program:
x = 2
print(x)
x = x + 2
print(x)
Output:
2
4
x = 2
print(x)
x = x + 2
print(x)
When a program is running, it flows from one step to the
next. As programmers, we set up “paths” for the program to
follow.
21
Conditional Steps
Output:
Smaller
Finis
Program:
x = 5
if x < 10:
print('Smaller')
if x > 20:
print('Bigger')
print('Finis')
x = 5
x < 10
?
print('Smaller')
x > 20
?
print('Bigger')
print('Finis')
Yes
No
No
Yes
22
Repeated Steps
Output:
5
4
3
2
1
Blastoff!
Program:
n = 5
while n > 0 :
print(n)
n = n – 1
print('Blastoff!')
n > 0
?
Loops (repeated steps) have iteration variables that
change each time through a loop.
No
print('Blastoff')
Yes
n = 5
print(n)
n = n -1
23
name = input('Enter file:')
handle = open(name, 'r')
counts = dict()
for line in handle:
words = line.split()
for word in words:
counts[word] = counts.get(word,0) + 1
bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = count
print(bigword, bigcount)
A short Python
“Story” about how to
count words in a file
A word used to read
data from a user
A sentence about
updating one of the
many counts
A paragraph about
how to find the
largest item in a list
24
Any Questions?
Slides taken from Scott Shawcroft, RyanTucker, and Paul Beck
25

01. Introduction to Python.pdf yessir sirski

  • 1.
    •Introductory concepts •Installing Python •InstallingPython IDEs •Your first Python Program Introduction to Python 1
  • 2.
    Introduction to Python Whatis Python programming language? 2
  • 3.
    Users vs. Programmers •Users see computers as a set of tools - word processor, spreadsheet, map, to-do list, etc. • Programmers learn the computer “ways” and the computer language • Programmers have some tools that allow them to build new tools • Programmers sometimes write tools for lots of users and sometimes programmers write little “helpers” for themselves to automate a task 3
  • 4.
    Why be aProgrammer? • To get some task done - we are the user and programmer  Clean up survey data; making numerical analysis • To produce something for others to use - a programming job  Fix a performance problem in the Sakai software  Add a guestbook to a web site 4
  • 5.
    What is Code?Software? A Program? • A sequence of stored instructions  It is a little piece of our intelligence in the computer  We figure something out and then we encode it and then give it to someone else to save them the time and energy of figuring it out • A piece of creative art - particularly when we do a good job on user experience 5
  • 6.
    Programs for Humans •Whilemusic is playing: • Left hand out and up • Right hand out and up • Flip Left hand • Flip Right hand • Left hand to right shoulder • Right hand to left shoulder • Left hand to back of head • Right ham to back of head • Left hand to right hit • Right hand to left hit • Left hand on left bottom • Right hand on right bottom • Wiggle • Wiggle • Jump https://www.youtube.com/watch?v=XiBYM6g8Tck 6
  • 7.
    Python! Python! • Created in1991 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) 7
  • 8.
    Python is usedeverywhere! 8
  • 9.
    Interpreted Languages • Interpreted –Not compiled like Java – Code is written and then directly executed by an interpreter – Type commands into interpreter and see immediate results Computer Runtime Environment Compiler Code Java: Computer Interpreter Code Python: 9
  • 10.
    Installing Python Windows: • DownloadPython from http://www.python.org • Install Python. • Run Idle from the Start Menu. Mac OS X: • Python is already installed. • Open a terminal and run python or run Idle from Finder. Linux: • Chances are you already have Python installed. To check, run python from the terminal. • If not installed, use the following. $ sudo apt-get install software- properties-common $ sudo add-apt-repository ppa:deadsnakes/ppa $ sudo apt-get update $ sudo apt-get install python3.8 10
  • 11.
    The Python Interpreter ThePython Interpreter • Allows you to type commands one-at-a-time and see results • A great way to explore Python's syntax – Repeat previous command: Alt+P 11
  • 12.
    How to runPython Windows • Run IDLE to use the interpret • Open a new window in IDLE to write and save programs 12
  • 13.
    How to runPython Unix • Start the interactive shell with python • Run a program with python /path/to/program.py 13
  • 14.
    Python Integrated Development Environments(IDEs)  There are lots of IDEs for Python programming. 14
  • 15.
    Installing Spyder  OnWindows, InstallWinPython scientific Python distribution.  It contains Spyder in this specific distribution.  On Ubuntu/Linux, run the following command. $sudo apt-get install spyder3 15
  • 16.
    Your First PythonProgram  With Python installed, we can write and run our first Python program.  Open a text editor software (like notepad) and write the following line of code into the file. print("Hello, World!")  Save the file as “filename.py” in your preferred folder or directory.  OpenWindows or Linux terminal and change current directory to the folder you stored your Python file.  Then run Python filename.py command. 16
  • 17.
    Elements of Python Vocabulary /Words -Variables and Reserved words  Sentence structure - valid syntax patterns  Story structure - constructing a program for a purpose 17
  • 18.
    Reserved Words You cannotuse reserved words as variable names / identifiers False class return is finally None if for lambda continue True def from while nonlocal and del global not with as elif try or yield assert else import pass break except in raise 18
  • 19.
    Interactive versus Script Interactive  You type directly to Python one line at a time and it responds  Script  You enter a sequence of statements (lines) into a file using a text editor and tell Python to execute the statements in the file 19
  • 20.
    Program Steps orProgram Flow  Like a recipe or installation instructions, a program is a sequence of steps to be done in order.  Some steps are conditional - they may be skipped.  Sometimes a step or group of steps is to be repeated.  Sometimes we store a set of steps to be used over and over as needed several places throughout the program. 20
  • 21.
    Sequential Steps Program: x =2 print(x) x = x + 2 print(x) Output: 2 4 x = 2 print(x) x = x + 2 print(x) When a program is running, it flows from one step to the next. As programmers, we set up “paths” for the program to follow. 21
  • 22.
    Conditional Steps Output: Smaller Finis Program: x =5 if x < 10: print('Smaller') if x > 20: print('Bigger') print('Finis') x = 5 x < 10 ? print('Smaller') x > 20 ? print('Bigger') print('Finis') Yes No No Yes 22
  • 23.
    Repeated Steps Output: 5 4 3 2 1 Blastoff! Program: n =5 while n > 0 : print(n) n = n – 1 print('Blastoff!') n > 0 ? Loops (repeated steps) have iteration variables that change each time through a loop. No print('Blastoff') Yes n = 5 print(n) n = n -1 23
  • 24.
    name = input('Enterfile:') handle = open(name, 'r') counts = dict() for line in handle: words = line.split() for word in words: counts[word] = counts.get(word,0) + 1 bigcount = None bigword = None for word,count in counts.items(): if bigcount is None or count > bigcount: bigword = word bigcount = count print(bigword, bigcount) A short Python “Story” about how to count words in a file A word used to read data from a user A sentence about updating one of the many counts A paragraph about how to find the largest item in a list 24
  • 25.
    Any Questions? Slides takenfrom Scott Shawcroft, RyanTucker, and Paul Beck 25