Fundamentals of Programming (Python) Basic...

19
Fundamentals of Programming (Python) Basic Concepts Ali Taheri Sharif University of Technology Spring 2018

Transcript of Fundamentals of Programming (Python) Basic...

Page 1: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

Fundamentals of Programming(Python)

Basic Concepts

Ali TaheriSharif University of Technology

Spring 2018

Page 2: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

Outline1. What is a “Computer”?

2. Computer System Organization

3. What is a “Computer Program”?

4. Programming Languages

5. The Python Language

2ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 3: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

What is a “Computer”?Device capable of ◦ Performing computations

◦ Making logical decisions

Works billions of times faster than human beings◦ Fastest supercomputers today perform hundreds of billions

of additions per second

3ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 4: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

Computer System Organization

4ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 5: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

Computer System Organization

5

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.

◦ “Administrative” section of computer

◦ Coordinates and supervises other sections

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 6: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

Computer System Organization

6

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)

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 7: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

Computer System Organization

7

Input Device:◦ “Receiving” section of computer

◦ Obtains data from:

• Usually a keyboard, mouse, disk or scanner

◦ Places data at disposal of other units

Output Device:◦ “Shipping” section of computer

◦ Puts processed info on:

• Screens, paper printouts, speakers

◦ Makes info available outside the computer

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 8: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

Computer System Organization

8

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!

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 9: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

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!

9ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 10: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

How a Program Runs?

10

◦ Program (in machine language) is loaded to RAM.◦ Program-Counter is set to first instruction.◦ CPU runs each instruction as follows:

◦ Fetches instruction determined by Program-Counter and decodes it.

◦ Gets the current value of each needed RAM cell,◦ Executes the instruction’s function on inputs,◦ Sends the result of instruction’s execution to RAM or

output devices if needed,◦ Increases Program-Counter to next instruction.

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 11: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

Programming Languages

11

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

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 12: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

Programming Languages

12

Fall into three categories◦ Machine languages

◦ Assembly languages

◦ High-level languages

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 13: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

Programming Languages

13

Machine Languages◦ Only language understood directly by computer

◦ Defined by computer’s hardware design◦ Machine-dependent

◦ Languages specific to particular computers

◦ Incomprehensible to human readers◦ Streams and numbers

◦ Ultimately reduced to 0s and 1s

◦ Instruct most elementary of operations

◦ Slow, tedious and error-prone

◦ Led to Assembly languages

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 14: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

Programming Languages

14

Assembly Languages◦ English-like abbreviations

◦ Represent elementary operations of computer

◦ Translated to machine language◦ Assemblers convert to machine language

◦ High-speed conversion

◦ More clear to human readers◦ Still tedious to use

◦ Many instructions for simple tasks

◦ Led to high-level languages

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 15: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

Programming Languages

15

High-Level Languages◦ Single statements accomplish

substantial tasks

◦ Translated to machine language◦ Compilers and Interpreters convert to machine

language

◦ Conversion takes considerable time

◦ Instructions comprehensible to humans◦ Look like everyday English

◦ Contain common mathematical notation

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 16: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

Programming Languages

16

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. But as compiled program is in system language, the compiled program could be ran just on a specific kind of platform.

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 17: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

The Python LanguageCreated by Guido van Rossum◦ Starting development on Dec, 1989

◦ First public version released on Feb, 1991

◦ Created as a scripting language for administrative tasks

◦ Based on All Basic Code (ABC) and Modula-3

Part of the open-source community◦ Growing community of Python developers

◦ Evolved into well-supported programming language

17ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 18: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

The Python LanguageMixture of compilation and interpretation◦ After development, the code, would be compiled to bytecode (a

middle language), and so is portable.

◦ In running, bytecode would be interpreted to machine.

◦ Machine runs the program (which is in machine language).

18ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 19: Fundamentals of Programming (Python) Basic Conceptsce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/1... · Basic Concepts Ali Taheri ... Outline 1. What is a “Computer”?

The Python LanguageDesigned to be portable and extensible◦ Originally implemented on UNIX

◦ Programs often can be ported from one operating system to another without any change

Syntax and design promote good programming practices and surprisingly rapid development times◦ Simple enough to be used by beginning programmers

◦ Powerful enough to attract professionals

Python is still being updated◦ Last version: 3.6, released on Dec, 2016

19ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018