Topic 3
Topic 3
With Python
Python Programming
CT108-3-1-PYP
TOPIC LEARNING OUTCOMES
• Introduction to Python
• Python as a programming language
• Interactive vs script programming
– COBOL
• business data
– LISP
• logic and AI
– BASIC
• a simple language
Users
• Literally someone who uses the computer or the smart-phone or the e-reader.
• Often is not an expert; he or she just owns the device and knows how it works.
• Example: Ali is an educated person, but computers are not his area of expertise
• See computers as a set of tools - word processor, spreadsheet, map, todo list, etc.
Programmers
• syntax: The set of legal structures and commands that can be used
in a particular programming language.
• output: The messages printed to the user by a program.
Trivia:
• Python is named after the BBC show “Monty Python’s Flying Circus” and
has nothing to do with reptiles
Many help forums still refer to Python2, so make sure you are
aware which version is being referenced
Object-oriented
• Structure supports such concepts as polymorphism, operation overloading, and multiple inheritance.
Indentation
• Is one of the greatest future in Python.
It's powerful
• Dynamic typing
• Built-in types and tools
• Library utilities
• Third party utilities (e.g. Numeric, NumPy, SciPy)
• Automatic memory management
It's portable
• Python runs virtually every major platform used today
• As long as you have a compatible Python interpreter installed, Python programs will run in the same manner, irrespective of platform.
It's mixable
Graphical
System User Internet
programming Interface Scripting
Programming
Gaming,
Component Database Images,
Integration Programming XML , Robot
and more
CT108-3-1-PYP Introduction to Programming With Python SLIDE 17 ‹#›
Compiling and Interpreting
>>> 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.
• Special use of + for string concatenation and % for string formatting (as in C’s printf)
x = x + 2
x = 2 print(x)
Assignment
Assignment Print
with
Statement statement
expression
Floats
x = 3.456
Strings
Use a newline to end a line of code Use \n when must go to next line prematurely
Useful when your code needs further explanation. Either for your future self and
anybody else.
Useful when you want to remove the code from execution but not permanently
When you call a python program from the command line the interpreter
evaluates each expression in the file
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
Syntax:
print("Message“)
print Expression
• Prints the given text message or expression value on the console and moves the cursor down to the next line.
print(Item1, Item2, ..., ItemN)
• Prints several messages and/or expressions on the same line.
Examples:
print("Hello, world!“)
age = 45
print("You have", 65 - age, "years until retirement“)
Output:
Hello, world!
You have 20 years until retirement
>>>print(‘hello’)
hello
>>>print(‘hello’ + ‘there’)
hellothere
x =2 Output:
2
4
print x
x = x +2
When a program is running, it flows from one step
to the next. We as programmers set up “paths”
print x for the program to follow
End
‹#›
Output:
x=5
Smaller
Finish
Yes print
x < 10?
‘Smaller’
No
Yes print
x > 20?
‘Bigger’
No
print
‘Finish’
n > 0? No print
‘Blastoff’
Yes Output:
5
print ‘n’ 4
3
2
n = n-1 1
Blastoff!
• Introduction to Python
• Python as a programming language
• Interactive vs script programming