🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

CT4 Programming Basics.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Programming Basics COURSE TOPIC 4 INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Programming Programming is the process of creating computer programs. Programming can be learned by discovering the techniques used by experienced programmers. These techniques are applicable t...

Programming Basics COURSE TOPIC 4 INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Programming Programming is the process of creating computer programs. Programming can be learned by discovering the techniques used by experienced programmers. These techniques are applicable to almost every programming language. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Programming Language A programming language is a computer language engineered to communicate instructions to a machine. Programs are created through programming languages to control the behavior and output of a machine through accurate algorithms, similar to the human communication process. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Levels of Programming Languages Machine Language Assembly Language High-Level Language INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Machine Language The fundamental language of the computer’s processor, also called Low Level Language. All programs are converted into machine language before they can be executed. Consists of combination of 0’s and 1’s that represent high and low electrical voltage. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Assembly Language A low level language that is similar to machine language. Uses symbolic operation code to represent the machine operation code. It needs a compiler or interpreter to convert this language to machine language. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING High-Level Language Computer programming languages that are easier to learn. Uses English like statements. Examples: Python, Java, C++, C#... A program written in the high-level language is called source program or source code For a computer to understand and execute a source program written in high-level language, it must be translated into machine language. – This translation is done using either compiler or interpreter. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Compiler A compiler translates a program from a high-level language to a low-level language the computer can run. You compile a program by running the compiler on the high-level-language version of the program called the source program. Compilers produce machine- or assembly-language programs called object programs. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Interpreter An interpreter is a program that reads source code one statement at a time, translates the statement into machine language, executes the machine language statement, then continues with the next statement. It is generally faster to run compiled code than to run a program under an interpreter. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Python Programming Language INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING History of Python Programming Language Python was conceived in the late 1980s and its implementation was started in December 1989 by Guido van Rossum at CWI in the Netherlands. Guido van Rossum—Creator and BDFL of Python Programming Language. (Image courtesy of Wikipedia.org) A successor to the ABC programming language capable of exception handling and interfacing with the Amoeba operating system INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Python Programming Language Python is an interpreted, high-level, general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Python is used for web development, AI, machine learning, operating systems, mobile application development, and some video games. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING The Compilation Process for Python Programs Python source code Python compilers compile source code into bytecode. bytecode When a Python program is run, the PVM translates bytecode to object code. object code INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Python Bytecode The Python compiler does not translate a Python program into machine language for a particular computer. Instead, it translates a Python program into bytecode. – Bytecode is the machine language for a hypothetical computer (or interpreter) called the Python Virtual Machine (PVM). INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Python Virtual Machine (PVM) Python Virtual Machine is the runtime engine of Python. As a Python program’s bytecode runs, the Python Virtual Machine (PVM) translates the bytecode into object code The execution of the bytecode by PVM is the last step of the Python Interpreter. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Object Code Object code is the language that a central processing unit can understand after it has been translated by the compiler or PVM from the programming source code. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Writing, Compiling and Running Python Programs Use an IDE (Integrated Development Environment) which combines a text editor with commands for writing, compiling and running Python programs. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Testing and Debugging Eliminate errors by avoiding them in the first place. – Carefully design algorithms and methods. – Carefully code everything into Python. Test your program with appropriate test cases (some where the answer is known), discover and fix any errors, then retest. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Errors An error in a program is called a bug. Eliminating errors is called debugging. Three kinds of errors: ▪ Syntax errors ▪ Runtime errors ▪ Logic errors INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Syntax Errors Every language has its own rules of grammar called Syntax Syntax errors (Grammatical mistakes) occur when the program statement violates one or more rules of syntax. The compiler catches syntax errors at the time of code development and prints an error message. Example: using a period where a program expects a comma INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Runtime Errors When a program is run and producing results that the computer cannot handle and cannot continue execution, run-time occurs. Errors that are detected when your program is running, but not during compilation When the computer detects an error, it terminates the program and prints an error message. Example: attempting to divide by 0 INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Logic Errors Errors that are not detected during compilation or while running, but which cause the program to produce incorrect results Unlike the other two types of errors, the computer cannot detect and provide clues for correcting Logic errors Example: an attempt to calculate a Fahrenheit temperature from a Celsius temperature by multiplying by 9/5 and adding 23 instead of 32 INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Click one of the links below to launch Python Online IDE: https://replit.com/languages/python3 https://www.programiz.com/python-programming/online-compiler/ Python Syntax Python syntax can be executed by writing directly in the Command Line: >>>print(“Hello, World!”) Hello, World! Or by creating a python file on the server, using the.py file extension, and running it in the Command Line: C:\Users\Your Name>python myfile.py INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Python Indentation Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Python Indentation Example (correct syntax): if 5 > 2: print(“Five is greater than two!”) Python will give you an error if you skip the indentation. For example: if 5 > 2: print(“Five is greater than two!”) INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Python Indentation The number of spaces is up to you as a programmer, but it has to be at least one. For example: if 5 > 2: print(“Five is greater than two!”) if 5 > 2: print(“Five is greater than two!”) You have to use the same number of spaces in the same block of code, otherwise Python will give you an error. For example: if 5 > 2: print(“Five is greater than two!”) print(“Five is greater than two!”) INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Reading Input in Python The input() function is used to gather data from the user. Syntax: variable_name = input([prompt]) Note: prompt is a string written inside the parenthesis that is printed on the screen. A string is written in single (‘ ‘) or double quotes (“ “). Example: person = input(“What is your name?”) What is your name? Carrey Note: Carrey is a value inputted by the user that is assigned to the variable person INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Print Output in Python The print() function allows the program to display text onto the console. Example: print(“This sentence is output to the screen.”) Output: This sentence is output to the screen. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Print Output in Python Another example is given below: a=5 print(‘The value of a is ’, a) Output: The value of a is 5 INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING str.format() Method Use str.format() method if you need to insert the value of a variable, expression or an object into another string and display it to the user as a single string. Syntax: str.format(p0, p1,..., k0=v0, k1=v1,...) Where: p0, p1,... are called as positional arguments k0, k1,... are keyword arguments with their assigned values of v0, v1,... respectively. str is a mixture of text and curly braces of indexed or keyword types. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Program to Demonstrate Positional Arguments Positional arguments are a list of arguments that can be accessed with an index of argument inside curly braces like {index}. Index value starts from zero. Example: country = input("In which country do you live in? ") print("I live in {0}".format(country)) Output: Which country do you live in? United Arab Emirates I live in United Arab Emirates INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Program to Demonstrate the Positional Change of Indexes of Arguments a = 10 b = 20 print("The values of a is {0} and b is {1}".format(a, b)) print("The values of b is {1} and a is {0}".format(a, b)) Output: The values of a is 10 and b is 20 The values of b is 20 and a is 10 INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Program to Demonstrate Keyword Argument Replaced with its Assigned Value Keyword arguments are a list of arguments of type keyword = value, that can be accessed with the name of the argument inside curly braces like {keyword}. Example: print("Give me {ball} ball".format(ball="tennis")) Output: Give me tennis ball INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING f-strings f-string is a Literal String Interpolation that is prefixed with “f”. The strings may contain replacement fields, which are expressions enclosed within curly braces {}. The expressions are replaced with their values. In the real world, it means that you need to specify the name of the variable inside the curly braces to display its value. An f at the beginning of the string tells Python to allow any currently valid variable names within the string. INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Code to Demonstrate the Use of f-strings with print() Function country = input("In which country do you live in? ") print(f"I live in {country}") Output: In which country do you live in? United Arab Emirates I live in United Arab Emirates INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Comments Python has commenting capability for the purpose of in-code documentation. Single line comments start with a #, and Python will render the rest of the line as a comment. Multiline comments are enclosed in triple quotes, ’’’ Example (single line comment): #This is single line Python comment Example (multiline comment) '''This is multiline comment in Python using triple quotes''' INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Whitespace Characters Character Purpose ‘ ‘ Space ‘\t’ Horizontal tab ‘\n’ New line ‘\v’ Vertical tab ‘\f’ Feed ‘\r’ Carriage return INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING Any Questions INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING

Use Quizgecko on...
Browser
Browser