Lec1 Python
Lec1 Python
print_lyrics():
x=5
print ('Hello’)
I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.
def print_lyrics():
print ("I'm a lumberjack, and I'm okay." )
print ('I sleep all night and I work all day.’)
print ('Yo’)
x=x+2 Hello
print (x) Yo
7
Hardware Architecture
Generic
Computer
Input Central
and Output Processing
Devices Unit
Secondary
Memory
Main
Memory
Definitions
• Central Processing Unit: Runs the Program - The CPU is
always wondering “what to do next”? Not the brains
exactly – not smart but very very fast
Source: http://commons.Wikimedia.org
Memory (RAM)
Source: http://commons.wikimedia.org
Hard Drive
Source: http://commons.Wikimedia.org
Python as a Language
Brief History of Python
• Invented in the Netherlands, early 90s by Guido van
Rossum
• Named after Monty Python
• Open sourced from the beginning, man-aged by Python
Software Foundation
• Considered a scripting language, but is much more
• Scalable, object oriented and functional from the
beginning
• Used by Google from the beginning
Python’s
place in the
Market
Setting up
Python
• When you make a mistake, the computer does not think you are “cute”. It
says “syntax error” - given that it “knows” the language and you are just
learning it.
• You must remember that you are intelligent and can learn - the computer
is simple and very fast - but cannot learn - so it is easier for you to learn
Python than for the computer to learn English...
Program Structurein Python
A program consists of two main parts:
For example:
def greet(name):
return f "Hello, {name}!"
For example:
print(greet("Alice"))
• This command calls the greet function and prints the result.
Evaluation of Definitions:
When you run a program, definitions are evaluated first,
meaning Python processes and stores them in memory so they
can be used later during execution.
For example:
For example:
>>> type(5)
int
>>> type(3.0)
float commands executed by Python interpreter
in a shell
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.
• 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
Elements of Python
print x
print 'Finish'
n=5
Repeated Steps
No Yes
n>0? Program:
print n n=5
while n > 0 :
n = n -1 print (n)
n=n–1
print ('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.
Stored (and reused) Steps
Program:
def
hello():
print 'Hello' def hello(): Output:
print 'Fun' print ('Hello’)
print ('Fun’) Hello
hello() hello() Fun
print ('Zip‘) Zip
print 'Zip' hello() Hello
Fun
hello()
We call these reusable pieces of code “functions”.
Summary