Introduction to Programming - ITM200 Lecture Notes PDF
Document Details
Uploaded by EffortlessArtNouveau1257
Ted Rogers School of Information Technology Management
2022
Zeinab Noorian
Tags
Related
Summary
These lecture notes provide an introduction to computer programming, focusing on fundamentals and the Python language. They cover topics such as program concepts, structure, and execution using a Python interpreter. The lecture materials also describe computer programming as a problem solving process.
Full Transcript
Fundamentals of Programming – ITM200 Lecture01 Introduction to Programming Zeinab Noorian PhD, Computer Science 1 Solving Problems using Computers ▪ Computers are used in nearly every realm of human activities spanning the...
Fundamentals of Programming – ITM200 Lecture01 Introduction to Programming Zeinab Noorian PhD, Computer Science 1 Solving Problems using Computers ▪ Computers are used in nearly every realm of human activities spanning the private, public and professional spheres. ▪ Computers have the special property of being programmable to automatically execute different set of instructions that specify what data to process and how to process it. These sets of instructions are called Computer Programs. ▪ General purpose programmability of computers enables the design of a wide spectrum of computer solutions that address the diversity of real-world problems. © Copyright Youcef Derbal, 2022 Computer programming is Problem Solving! 1. Understand the problem (Analysis) 2. Devise a solution / Algorithm (Design) - step by step procedure for the computer to execute towards solving the problem. 3. Code the solution using a computer language (Implementation) 4. Test & Debug (Testing) 5. Iterate through the previous steps as needed to achieve a solution that meets the requirements of the solution. © Copyright Youcef Derbal, 2022 Computer Programs ▪ Program : set of computer executable instructions ▪ Computer Instructions are written ( by programmers) in a manner similar to writing an essay and are stored in a source code file. ▪ The source code file can be read and edited using a regular text editor such as MS Notepad. © Copyright Youcef Derbal, 2022 Compilers, interpreters and Executable ▪ Program execution requires the source code to be compiled or interpreted (e.g. Python) ▪ C++ and Java, the source code need to be compiled using a compiler (a special software ), into an executable file ▪ Python programs (source code) are directly executable by a Python interpreter © Copyright Youcef Derbal, 2022 Compilers, interpreters and Executable Many languages require you to compile (translate) your program into a form that the machine understands. compile execute source code byte code output Hello.java Hello.class Python is instead directly interpreted into machine instructions. interpret source code output Hello.py 7 Python Programs The source code file may be given an arbitrarily chosen name such as welcome.py. The part of the name that follows the period is called an extension and it refers to the computer programming language being used, py for Python. © Copyright Youcef Derbal, 2022 Python Interpreter Python programs such as welcome.py are executed on the fly using a Python interpreter, An interpreter is a specialized program that has to be installed on your computer. The interpreter translates the source code (text like), into a machine language (sequences of 0s and 1s) that can be executed by computers. © Copyright Youcef Derbal, 2022 Interactive versus 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 Python Scripts Interactive Python is good for experiments and programs of 3-4 lines long. 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 a convention, we add “.py” as the suffix on the end of these files to indicate they contain Python. Programing Basics code or source code: The sequence of instructions in a program. syntax: The set of legal structures and commands that can be used in a particular programming language. output: The messages printed to the user by a program. console: The text box onto which output is printed. Some source code editors pop up the console as an external window, and others contain their own console window. 1 Anatomy of a Python Program The first two lines of the program start with a hashtag (#) signaling that their content are comments and hence have no bearing on the program’s functional purpose. The instruction responsible for the display of the “Welcome …” message is a call to the Python built-in function print, which displays whatever is provided as argument. In this case the argument to the function print is the string “Hello World!”. © Copyright Youcef Derbal, 2022 Python Programming Computer programming languages such as Python and Java are said to be high-level programming languages - (instructions are similar to sentences in English) Like natural languages, Python has its own vocabulary (list of reserved words) and grammar (rules for forming valid instructions/expressions). © Copyright Youcef Derbal, 2022 Reserved Words You cannot use reserved words as variable names / identifiers False class return is finally None if for lambda continue True def from while nonlocal and del global not with as elif try or yield assert else import pass break except in raise Sentences or Lines x = 2 Assignment statement x = x + 2 Assignment with expression print(x) Print statement Variable Operator Constant Function Program Steps or Program Flow A program is a sequence of steps to be done in order. Some steps are conditional - they may be skipped. Sometimes a step or group of steps is 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=2 Program: Output: print(x) x = 2 print(x) 2 x=x+2 x = x + 2 4 print(x) print(x) When a program is running, it flows from one step to the next. As programmers, we set up “paths” for the program to follow. x=5 Conditional Steps x < 10 Yes ? Program: print('Smaller') No Output: x = 5 if x < 10: Ye Smaller x > 20 s print('Smaller') Finis ? if x > 20: No print('Bigger') print('Bigger') print('Finish') print('Finish') n=5 Repeated Steps No Yes Output: n>0 ? Program: 5 print(n) n = 5 4 while n > 0 : print(n) 3 n = n -1 n = n – 1 2 print('Blastoff!') 1 Blastoff! Loops (repeated steps) have iteration variables that print('Blastoff') change each time through a loop. name = input('Enter file:') handle = open(name, 'r') Sequential Repeated counts = dict() for line in handle: Conditional words = line.split() for word in words: counts[word] = counts.get(word,0) + 1 bigcount = None bigword = None for word,count in counts.items(): if bigcount is None or count > bigcount: bigword = word bigcount = count print(bigword, bigcount) Program Flow Representation oval: start / end parallelogram: input / output rectangle: calculations diamond: selection structures Example # start num = input("Enter a number: ") num = float(num) num_plus_2 = num + 2 print(num_plus_2) # end Example # start num = input('Enter a number: ') num = float(num) if num>0: print('num greater than zero') if num