[go: up one dir, main page]

0% found this document useful (0 votes)
19 views60 pages

Lecture 1

CSE 211 is an undergraduate course focused on basic programming concepts using Python, covering topics such as variables, data types, control structures, and object-oriented programming. The course requires a minimum of 75% attendance and includes a grading scheme based on exams, assignments, and quizzes. Students are expected to demonstrate programming skills and problem-solving abilities by the end of the course.

Uploaded by

Marwan Zaki
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)
19 views60 pages

Lecture 1

CSE 211 is an undergraduate course focused on basic programming concepts using Python, covering topics such as variables, data types, control structures, and object-oriented programming. The course requires a minimum of 75% attendance and includes a grading scheme based on exams, assignments, and quizzes. Students are expected to demonstrate programming skills and problem-solving abilities by the end of the course.

Uploaded by

Marwan Zaki
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/ 60

Department of Computer Science & Engineering

CSE-211 Computer Programming


Instructor : Dr. Rami Zewail
GENERAL INFORMATION

CSE 211
Level Under-Graduate
Pre-requisites None
Course web-page TBD
Attendance Policy A minimum of 75% attendance is expected.
Attention: Turn your cell phone Off or silent before class and Put it

away.
Instructor contact rami.zewail@ejust.edu.eg

Grading Scheme Final Exam - 50 %


Mid Term Exam - 20 %
Assignments - 20 % Quizzes– 10%
GENERAL INFORMATION

CSE 211/212 –Computer Programming

Course Overview Introduction to basic programming concepts; basic syntax and semantics of a
modern high level language; variables; data types; assignment, arithmetic and
logic operations; input/output operations; branching and iterative control
structures; objects and classes; containers (lists, sets, maps); functions and
parameter passing; recursion; debugging and testing.

Expected Outcome  Students demonstrate basic programming language skills including familiarity
with data types, variables, conditional statements, loops, and file handling.

 Students demonstrate ability to use python to solve different computational


problems.

 Student demonstrate basic understanding to modular design and object-


oriented programming.
GENERAL INFORMATION

CSE 211/212 –Computer Programming


Recommended 1. Python Programming: An Introduction to Computer Science, John Zelle

References 2. Introduction to Programming Using Python 3, Daniel Liang.

3. Introduction to Computer Science Using Python: A Computational Problem-


Solving Focus, Charles Dierbach

4.Think Python,2nd Edition

5. Introduction to Computation and Programming Using Python, Second


Edition, John V. Guttag
Disclaimer Slides throughout the course are based on the textbooks above and some
slides will be modified from the slides made available by the above
mentioned textbook publishers & authors.

Copyright © 2016 Elsevier


How To Maximize your Benefit of CSE211 ?

• Select one or more of the reference textbooks and read


relevant topics after each lecture

• Come to every lecture/lab.

• Practice writing a lot of programs.


– Work independently
– Turn in assignments on time.
How To be Good Programmer ?

• Foundation Comes First

• There is no such thing as “Gifted Programmer”, You need to


work hard.
Lecture One : Introduction
Objectives

• To understand the respective roles of hardware and software in


a computing system.
• To learn what computer scientists study and the techniques
that they use.
• To understand the basic design of a modern computer.

8
Python Programming, 3/e
Objectives (cont.)

• To understand the form and function of computer


programming languages.
• To begin using the Python programming language.
• To learn about chaotic models and their implications for
computing.

9
Python Programming, 3/e
The Universal Machine

• A modern computer can be defined as “a machine that stores


and manipulates information under the control of a changeable
program.”
• Two key elements:
– Computers are devices for manipulating information.
– Computers operate under the control of a changeable program.

10
Python Programming, 3/e
The Universal Machine

• What is a computer program?


– A detailed, step-by-step set of instructions telling a computer what to
do.
– If we change the program, the computer performs a different set of
actions or a different task.
– The machine stays the same, but the program changes!

11
Python Programming, 3/e
The Universal Machine

• Programs are executed, or carried out.


• All computers have the same power, with suitable
programming, i.e. each computer can do the things any other
computer can do.

12
Python Programming, 3/e
Program Power

• Software (programs) rule the hardware (the physical machine).


• The process of creating this software is called programming.

• Why learn to program?


– Fundamental part of computer science
– Having an understanding of programming helps you have an
understanding of the strengths and limitations of computers.

13
Python Programming, 3/e
Program Power

– Helps you become a more intelligent user of computers


– It can be fun!
– Form of expression
– Helps the development of problem solving skills, especially in
analyzing complex systems by reducing them to interactions between
simpler systems.
– Programmers are in great demand!

14
Python Programming, 3/e
What is Computer Science?

• It is not the study of computers!


“Computers are to computer science what telescopes are to
astronomy.” –
E. Dijkstra
• The question becomes, “What processes can be described?”
• This question is really, “What can be computed?”

15
Python Programming, 3/e
What is Computer Science?

• Design
– One way to show a particular problem can be solved is to actually
design a solution.
– This is done by developing an algorithm, a step-by-step process for
achieving the desired result.
– One problem – it can only answer in the positive. You can’t prove a
negative!

16
Python Programming, 3/e
What is Computer Science?

• Analysis
– Analysis is the process of examining algorithms and problems
mathematically.
– Some seemingly simple problems are not solvable by any algorithm.
These problems are said to be unsolvable.
– Problems can be intractable if they would take too long or take too
much memory to be of practical value.

17
Python Programming, 3/e
Computer Algorithms
• An algorithm is a finite number of clearly
described, unambiguous “doable” steps
that can be systematically followed to
produce a desired result for given input in
a finite amount of time (that is, it
eventually terminates).

• The word “algorithm” is derived from the


ninth-century Arab mathematician, Al-
Khwarizmi who worked on “written
processes to achieve some goal.”

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms 18
The First Computer Programmer

• The first computer programs ever


written were for a mechanical
computer designed by Charles
Babbage in the mid-1800s. The
person who wrote these programs
was a woman, Ada Lovelace, who
was a talented mathematician.
Thus, she is referred to as “the
first computer programmer.”

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms 19
The First Computer “BUG”

• In 1947, engineers working on the


Mark II
• computer at Harvard University
found a moth stuck in one of the
components, causing the machine
to malfunction. They taped the
insect in their logbook and labeled
it “first actual case of bug being
found.”

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms 20
Hardware Basics

21
Python Programming, 3/e
Hardware Basics

• The central processing unit (CPU) is the “brain” of a computer.


– The CPU carries out all the basic operations on the data.
– Examples: simple arithmetic operations, testing to see if two
numbers are equal.

22
Python Programming, 3/e
Hardware Basics

• Memory stores programs and data.


– CPU can only directly access information stored in main
memory (RAM or Random Access Memory).
– Main memory is fast, but volatile, i.e. when the power is
interrupted, the contents of memory are lost.
– Secondary memory provides more permanent storage:
magnetic (hard drive), flash (SSD, USB memory), optical (CD,
DVD)

23
Python Programming, 3/e
Hardware Basics

• Input devices
– Information is passed to the computer through keyboards, mice, etc.
• Output devices
– Processed information is presented to the user through the monitor,
printer, etc.

24
Python Programming, 3/e
Hardware Basics

• Fetch-Execute Cycle
– First instruction retrieved from memory
– Decode the instruction to see what it represents
– Appropriate action carried out.
– Next instruction fetched, decoded, and executed.
– Lather, rinse, repeat!

25
Python Programming, 3/e
Programming Languages

• Natural language has ambiguity and imprecision problems when


used to describe complex algorithms.
– Programs expressed in an unambiguous, precise way using programming
languages.
– Every structure in programming language has a precise form, called its
syntax
– Every structure in programming language has a precise meaning, called
its semantics.

26
Python Programming, 3/e
Programming Languages

• Programming language like a code for writing the instructions


the computer will follow.
– Programmers will often refer to their program as computer code.
– Process of writing an algorithm in a programming language often
called coding.

27
Python Programming, 3/e
Programming Languages

• High-level computer languages


– Designed to be used and understood by humans
• Low-level language
– Computer hardware can only understand a very low level language
known as machine language

28
Python Programming, 3/e
Programming Languages

• Add two numbers:


– Load the number from memory location 2001 into the CPU
– Load the number from memory location 2002 into the CPU
– Add the two numbers in the CPU
– Store the result into location 2003
• In reality, these low-level instructions are represented in binary
(1’s and 0’s)

29
Python Programming, 3/e
Programming Languages

• High-level language
c=a+b
• This needs to be translated into machine language that the
computer can execute.
• Compilers convert programs written in a high-level language
into the machine language of some computer.

30
Python Programming, 3/e
Programming Languages

31
Python Programming, 3/e
Programming Languages

• Interpreters simulate a computer that understands a high-level


language.
• The source program is not translated into machine language all
at once.
• An interpreter analyzes and executes the source code
instruction by instruction.

32
Python Programming, 3/e
Programming Languages

33
Python Programming, 3/e
Programming Languages

• Compiling vs. Interpreting


– Once program is compiled, it can be executed over and over without
the source code or compiler. If it is interpreted, the source code and
interpreter are needed each time the program runs

– Compiled programs generally run faster since the translation of the


source code happens only once.

34
Python Programming, 3/e
Programming Languages

– Interpreted languages are part of a more flexible programming


environment since they can be developed and run interactively

– Interpreted programs are more portable, meaning the executable code


produced from a compiler for a Pentium won’t run on a Mac, without
recompiling. If a suitable interpreter already exists, the interpreted code
can be run with no modifications.

35
Python Programming, 3/e
Introduction to Python programming Language

• The Python Programming Language was created by


Guido van Rossum. It was first released in the early
1990s.

• Its name comes from a 1970s British comedy sketch


show called Monty Python’s Flying Circus. (An example
of their work is The Argument Clinic).

• Companies and organizations that use Python include


YouTube, Google, Yahoo and NASA.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms 36
Introduction to Python

When you start Python, you will see something


like:
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>

37
Python Programming, 3/e
Introduction to Python

When you start Python, you will see something


like:
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>

38
Python Programming, 3/e
Introduction to Python

• The “>>>” is a Python prompt indicating that Python is ready for us to


give it a command. These commands are called statements.
• >>> print("Hello, world")
Hello, world
>>> print(2+3)
5
>>> print("2+3=", 2+3)
2+3= 5
>>>

39
Python Programming, 3/e
Introduction to Python

• Usually we want to execute several statements together that


solve a common problem. One way to do this is to use a
function.
• >>> def hello():
print("Hello")
print("Computers are Fun")

>>>

40
Python Programming, 3/e
Introduction to Python

• >>> def hello():


print("Hello")
print("Computers are Fun")

>>>

• The first line tells Python we are defining a new function called hello.
• The following lines are indented to show that they are part of the hello
function.
• The blank line (hit enter twice) lets Python know the definition is
finished.

41
Python Programming, 3/e
Introduction to Python
• >>> def hello():
print("Hello")
print("Computers are Fun")
>>>
• Notice that nothing has happened yet! We’ve defined the function, but
we haven’t told Python to perform the function!

• A function is invoked (or called) by typing its name.


• >>> hello()
Hello
Computers are Fun
>>>

42
Python Programming, 3/e
Introduction to Python

• What’s the deal with the ()’s?


• Commands can have changeable parts called
parameters (or arguments) that are placed between
the ()’s.
• >>> def greet(person):
print("Hello",person)
print ("How are you?")

>>>

43
Python Programming, 3/e
Introduction to Python

• >>> greet("Terry")
Hello Terry
How are you?
>>> greet("Paula")
Hello Paula
How are you?
>>>
• When we use parameters, we can customize the output of our
function.

44
Python Programming, 3/e
Introduction to Python

• When we exit the Python prompt, the functions we’ve defined cease to exist!
• Programs are usually composed of functions, modules, or scripts that are saved on disk
so that they can be used again and again.
• A module file is a text file created in text editing software (saved as “plain text”) that
contains function definitions.
• A programming environment is designed to help programmers write programs and
usually includes automatic indenting, highlighting, etc.

45
Python Programming, 3/e
Introduction to Python

# File: chaos.py
# A simple program illustrating chaotic behavior

def main():
print("This program illustrates a chaotic function")
x = eval(input("Enter a number between 0 and 1: "))
for i in range(10):
x = 3.9 * x * (1 - x)
print(x)

main()

• We’ll use filename.py when we save our work to indicate it’s a Python
program.
• In this code we’re defining a new function called main.
• The main() at the end tells Python to run the code.

46
Python Programming, 3/e
Introduction to Python

>>>
This program illustrates a chaotic function
Enter a number between 0 and 1: .5
0.975
0.0950625
0.335499922266
0.869464925259
0.442633109113
0.962165255337
0.141972779362
0.4750843862
0.972578927537
0.104009713267
>>>

47
Python Programming, 3/e
Inside a Python Program
# File: chaos.py
# A simple program illustrating chaotic behavior

• Lines that start with # are called comments


• Intended for human readers and ignored by Python
• Python skips text from # to end of line

48
Python Programming, 3/e
Inside a Python Program
def main():

• Beginning of the definition of a function called main


• Since our program has only this one module, it could have
been written without the main function.
• The use of main is customary, however.

49
Python Programming, 3/e
Inside a Python Program
print("This program illustrates a chaotic function")

• This line causes Python to print a message introducing the


program.

50
Python Programming, 3/e
Inside a Python Program
x = eval(input("Enter a number between 0 and 1: "))

• x is an example of a variable
• A variable is used to assign a name to a value so that we can
refer to it later.
• The quoted information is displayed, and the number typed in
response is stored in x.

51
Python Programming, 3/e
Inside a Python Program
for i in range(10):

• For is a loop construct


• A loop tells Python to repeat the same thing over and over.
• In this example, the following code will be repeated 10 times.

52
Python Programming, 3/e
Inside a Python Program

x = 3.9 * x * (1 - x)
print(x)

• These lines are the body of the loop.


• The body of the loop is what gets repeated each
time through the loop.
• The body of the loop is identified through
indentation.
• The effect of the loop is the same as repeating
these two lines 10 times!
53
Python Programming, 3/e
Inside a Python Program

x = 3.9 * x * (1 - x)
for i in range(10): print(x)
x = 3.9 * x * (1 - x)
x = 3.9 * x * (1 - x)
print(x)
print(x) x = 3.9 * x * (1 - x)
print(x)
x = 3.9 * x * (1 - x)
print(x)
x = 3.9 * x * (1 - x)

• These are
print(x)
x = 3.9 * x * (1 - x)
print(x)

equivalent! x = 3.9 * x * (1 - x)
print(x)
x = 3.9 * x * (1 - x)
print(x)
x = 3.9 * x * (1 - x)
print(x)
x = 3.9 * x * (1 - x)
print(x)

54
Python Programming, 3/e
Inside a Python Program
x = 3.9 * x * (1 - x)

• This is called an assignment statement


• The part on the right-hand side (RHS) of the "=" is a mathematical
expression.
• * is used to indicate multiplication
• Once the value on the RHS is computed, it is stored back into (assigned)
into x

55
Python Programming, 3/e
Inside a Python Program
main()

• This last line tells Python to execute the code in the function
main

56
Python Programming, 3/e
Chaos and Computers

• The chaos.py program:


def main():
print("This program illustrates a chaotic function")
x = eval(input("Enter a number between 0 and 1: "))
for i in range(10):
x = 3.9 * x * (1 - x)
print(x)
main()

• For any given input, returns 10 seemingly random numbers between 0


and 1
• It appears that the value of x is chaotic

57
Python Programming, 3/e
Chaos and Computers

• The function computed by program has the general form k ( x)(1  x )


where k is 3.9
• This type of function is known as a logistic function.
• Models certain kinds of unstable electronic circuits and population variation
under limiting conditions.
• Very small differences in initial value can have large differences in the
output.

58
Python Programming, 3/e
Chaos and Computers

Input: 0.25 Input: 0.26


0.73125 0.75036
0.76644140625 0.73054749456
0.698135010439 0.767706625733
0.82189581879 0.6954993339
0.570894019197 0.825942040734
0.955398748364 0.560670965721
0.166186721954 0.960644232282
0.540417912062 0.147446875935
0.9686289303 0.490254549376
0.118509010176 0.974629602149
59
Python Programming, 3/e
Thank You

You might also like