Python : programming language
CT010-3-1 Python Programming
Topic & Structure of the lesson
• Problem Solving Using Programmed
Solutions
– Introduction to Python
– Python as a programming language
– Interactive vs script programming
CT010-3-1 Fundamentals of Software Development Introduction to Python
Learning outcomes
• At the end of this lecture you should be
able to:
– Develop a problem-based strategy for creating and applying
programmed solutions.
CT010-3-1 Fundamentals of Software Development Introduction to Python
Key terms you must be able to
use
• If you have mastered this topic, you
should be able to use the following terms
correctly in your assignments and exams:
– Program
– Interactive
– Script
CT010-3-1 Fundamentals of Software Development Introduction to Python
Computers want to be helpful...
• Computers are built for one purpose - to
do things for us
• But we need to speak their language to
describe what we want done
• Users have it easy - someone already
put many different programs
(instructions) into the computer and
users just pick the ones we want to use
CT010-3-1 Fundamentals of Software Development Introduction to Python
Users .vs. Programmers
• Users see computers as a set of tools - word
processor, spreadsheet, map, todo 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
CT010-3-1 Fundamentals of Software Development Introduction to Python
What is Code? Software? A
Program?
• A sequence of stored instructions
– It is a little piece of our intelligence in the computer
– It is a little piece of our intelligence we can give to
others - 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
CT010-3-1 Fundamentals of Software Development Introduction to Python
Talking to Python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23
2016, 08:06:12) [MSC v.1900 64 bit (AMD64)]
on win32
Type "copyright", "credits" or "license()"
for more information.
>>>
CT010-3-1 Fundamentals of Software Development Introduction to Python
>>> 1 + 2
3
>>> 2 * 3
6
>>> x = 1
>>> print(x)
1
>>> x = x + 1
>>> print(x)
2
>>> exit()
This is a good test to make sure that you have Python
correctly installed. Note that quit() also works to end the
interactive session.
CT010-3-1 Fundamentals of Software Development Introduction to Python
Elements of Python
• Vocabulary / Words - Variables and Reserved words
(Chapter 2)
• Sentence structure - valid syntax patterns (Chapters
3-5)
• Story structure - constructing a program for a purpose
CT010-3-1 Fundamentals of Software Development Introduction to Python
Variable Names
• Choose a meaningful name
• Can be as long as you like
• Can contain both letters and numbers
• Cannot begin with a number
• Cannot use keywords
• Legal to use uppercase, however all lowercase
is the convention
• Underscore can be used for multi-word variable
name:
name_of_student
CT010-3-1 Fundamentals of Software Development Introduction to Python ‹#›
Keywords or Reserved Words
• Keywords are words reserved by python that have
predefined meaning. You can not use reserved
words as variable names / identifiers
False class finally is
return
None continue for lambda
try
True def from
nonlocal while
and del global not with
as elif if or
yield
CT010-3-1 Fundamentals of Software Development Introduction to Python
Sentences or Lines
• x=2 Assignment Statement
• x=x+2 Assignment with expression
• print(x) Print statement
CT010-3-1 Fundamentals of Software Development Introduction to Python
Python Scripts
Interactive Python is good for experiments and
programs of 3-4 lines long
But most programs are much longer so we type
them into a file and tell python to run the
commands in the file.
In a sense we are “giving Python a script”
As convention, we add “.py” as the suffix on the
end of these files to indicate they contain Python
CT010-3-1 Fundamentals of Software Development Introduction to Python
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
CT010-3-1 Fundamentals of Software Development Introduction to Python
3. 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 are to
be repeated
• Sometimes we store a set of steps to be
used over and over as needed in several
places throughout the program
CT010-3-1 Fundamentals of Software Development Introduction to Python
Sequential Steps
x=2
Program:
Output:
print x x=2
print(x) 2
x=x+2 4
x=x+2 print(x)
print x
When a program is running, it flows from one step
to the next. We as programmers set up “paths” for
the program to follow.
CT010-3-1 Fundamentals of Software Development Introduction to Python
x=5
Conditional Steps
Yes
X < 10 ?
print
'Smaller' Program: Output:
x=5 Smaller
Yes if x < 10: Finis
X > 20 ? print('Smaller’)
if x > 20:
print print('Bigger‘)
'Bigger' print( 'Finish‘)
print 'Finish'
CT010-3-1 Fundamentals of Software Development Introduction to Python
n=5
Repeated Steps
No Yes
n>0?
Output:
Program:
print n
5
n=5
4
while n > 0 :
3
print(n)
n = n -1 n=n–1
2
1
print('Blastoff!‘)
Blastoff!
print
'Blastoff'
Loops (repeated steps) have iteration variables that
change each time through a loop. Often these iteration
variables go through a sequence of numbers.
CT010-3-1 Fundamentals of Software Development Introduction to Python
Next Session
• Problem Solving Using Programmed
Solutions
– Variables, expressions and statements in
python
• Rules for naming variables
• Numeric expressions
• Operator precedence
• Statements in python
• Reserved words
CT010-3-1 Fundamentals of Software Development Introduction to Python