[go: up one dir, main page]

0% found this document useful (0 votes)
3 views36 pages

Lec1 Python

The document provides an introduction to programming, emphasizing the distinction between users and programmers, and the importance of learning a programming language like Python. It covers basic concepts such as code structure, program flow, and the role of the Central Processing Unit (CPU) in executing instructions. Additionally, it highlights Python's history, its market position, and the difference between interactive and script-based programming.

Uploaded by

zaranalikhan2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views36 pages

Lec1 Python

The document provides an introduction to programming, emphasizing the distinction between users and programmers, and the importance of learning a programming language like Python. It covers basic concepts such as code structure, program flow, and the role of the Central Processing Unit (CPU) in executing instructions. Additionally, it highlights Python's history, its market position, and the difference between interactive and script-based programming.

Uploaded by

zaranalikhan2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Intro to Programming

Engr. Mona Waseem


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
What What What
• Users have it easy - someone already Next? Next? Next?
put many different programs
(instructions) into the computer and What What What
Next? Next? Next?
users just pick the ones we want to use
Users .vs. Programmers
• Users see computers as a set of tools - word processor, spreadsheet,
maps, to-do list, email, 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
Why be a programmer?

• To get some task done - we are the user and programmer


• Help analyze and process data (many programs have APIs)
• To produce something for others to use – write programs
• Fix an issue with a program
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
while music is playing:
Left hand out and up Programs for
Right hand out and up
Flip Left hand Humans...
Flip Right hand
Left hand to right shoulder
Right hand to left shoulder
Left hand to back of head
Right hand 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
Program for a Computer

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

• Input Devices: Keyboard, Mouse, Touch Screen


• Output Devices: Screen, Speakers, Printer, DVD Burner
• Main Memory: Fast small temporary storage - lost on reboot - aka Random
Access Memory (RAM)

• Secondary Memory: Slower large permanent storage - lasts until deleted -


disk drive / memory stick
Central Processing Unit (CPU)

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

You can download Python at no charge from www.python.org


17
Python: A Computer Language
• We need to learn the Python language so we can communicate our
instructions to Python. In the beginning we will make lots of mistakes

• 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:

Definitions: These are the parts of the code where you


define variables, functions, classes, or other entities.

For example:

def greet(name):
return f "Hello, {name}!"

• Here, greet(name) is a definition of a function.


Program Structure
Commands: These are the statements or instructions that
execute or invoke actions, such as calling functions or
performing operations.

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:

def add(a, b): return a + b

Python doesn't calculate anything yet; it simply stores the


definition of add.
Execution of Commands:
Once the definitions are evaluated, commands are executed,
meaning Python runs the instructions line by line in the order they
appear.

For example:

result = add(3, 4) # Calls the defined function `add`


with arguments 3 and 4
print(result) # Prints 7
Talking to Python
Python Interpreter in a Shell:
A shell is an interactive interface for executing Python code. In
this environment:
•Definitions persist until the session ends or they are
explicitly removed.
•Commands can be executed immediately, allowing you to
see results or debug step by step.
For instance, in a Python shell :
>>> def square(x):
... return x * x
>>> square(5) # Executes the command
25
This means the interpreter evaluates the function definition first,
then executes the command to compute square(5).
Prompt
• int – represent integers, ex. 5
• float – represent real numbers, ex. 3.27
• bool – represent Boolean values True and False
• None Type – special and has one value, None
• Can use type() to see the type of an object

>>> 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.

• 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
Interactive vs. 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
Elements of Python

• Vocabulary / Words - Variables and Reserved words


• Sentence structure - valid syntax patterns
• Story structure - constructing a program for a purpose
Reserved Words

• You can not use reserved words as variable names / identifiers

and del for is raise assert elif from lambda


return break else global not try class except
if or while continue exec import
pass yield def finally in print
Statements

x=2 Assignment Statement


x=x+2 Assignment with expression
print (x) Print statement

Variable Operator Constant Reserved Word


Program Steps and Program Flow
• Like a recipe or installation instructions, a program is a sequence of
steps to be done in sequence

• Some steps are conditional


• 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
several places throughout the program
Sequential Steps
x=1 x=1
print (x )
print x x=x+1
Print (x)
x=x+1

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.
x=5
Conditional Steps
Yes
X < 10 ?
Program:
print 'Smaller'
x= 5
if x < 10 :
Yes print (‘Smaller’)
X > 20 ?
if x > 20 :
print 'Bigger' print (‘Bigger’)
print (‘Finish’)

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

• This is a quick overview of Python basics


• We will revisit these concepts throughout the course
• Focus on the big picture

You might also like