Fundamentals of Programming (Python) Basic Concepts Ali Taheri Sharif University of Technology
Outline 1. What is a Computer? 2. Computer System Organization 3. What is a Computer Program? 4. Programming Languages 5. The Python Language 2
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 3
Computer System Organization 4
Computer System Organization 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 5
Computer System Organization 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) 6
Computer System Organization 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 7
Computer System Organization 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! 8
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! 9
How a Program Runs? 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. 10
Programming Languages 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. 11
Programming Languages Fall into three categories Machine languages Assembly languages High-level languages 12
Programming Languages 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 13
Programming Languages 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 14
Programming Languages 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 15
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. But as compiled program is in system language, the compiled program could be ran just on a specific kind of platform. 16
The Python Language Created 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 17
The Python Language Mixture 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). 18
The Python Language Designed 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 19