Csc 102 Lecture 4 Introduction to Programming (Python) PDF

Document Details

HardierIntegral9696

Uploaded by HardierIntegral9696

LASU

Dr. Zubair Adam

Tags

python programming programming languages computer programming computer science

Summary

This document is a lecture on Introduction to Programming (Python). It covers fundamental concepts of computer programming in the Python language. The presentation includes information on programming environments, syntax, operators, data types and control statements.

Full Transcript

CSC 102 LECTURE 4 INTRODUCTION TO PROGRAMMING (PYTHON) DR. ZUBAIR ADAM OVERVIEW  Introduction to the core concepts of computing: Problems and problem-solving.  The identification of problems and types of problems (routine problems and non-routine problems). ...

CSC 102 LECTURE 4 INTRODUCTION TO PROGRAMMING (PYTHON) DR. ZUBAIR ADAM OVERVIEW  Introduction to the core concepts of computing: Problems and problem-solving.  The identification of problems and types of problems (routine problems and non-routine problems).  Method of solving computing problems (introduction to algorithms and heuristics).  Solvable and unsolvable problems.  Solution techniques of solving problems (abstraction, analogy, brainstorming, trial and error, hypothesis  Testing, reduction, literal thinking, means end analysis, method of focal object, morphological analysis, research, root cause analysis, proof, divide and conquer).  General Problem-solving process.  Solution formulation and design: flowchart, pseudocode, decision table, decision tree. Implementation, evaluation and refinement.  Programming in Python etc Programming A computer program is a sequence of instructions written using a Computer Programming Language to perform a specified task by the computer. If you understood what a computer program is, then we will say: the act of writing computer programs is called computer programming. There are hundreds of programming languages, which can be used to write computer programs and following are a few of them: Python, Java, C++, e.t.c Level of Programming  Programming languages are classified into five major categories:  Machine languages (first generation languages),  Assembly languages (second generation languages),  Third generation languages,  Fourth generation languages  Natural languages.  Machine and assembly languages are referred to as Low level languages;  Third generation, fourth generation and natural languages are categorized as High level languages. Fundamentals of Programming Programming Environment Comments Basic Syntax Basic Operator Data Types Decision Making Variables Loops Keywords Collection (Arrays, List, Tuple) Numbers Functions Characters and Strings File I/O Environment Setup  Programming Environment Setup is not an element of any Programming Language, it is the first step to be followed before setting on to write a program.  When we say Programming Environment Setup, it simply implies a base/platform on top of which we can do our programming.  The choice of environment setup can be offline/online, mobile/desktop, compiler/interpreter.  Our environment for this course is on Colab platform online.  https://colab.research.google.com Sample Program Execution Python syntax can be executed by writing directly in the Command Line print(‘Hello World’) Our Programming Environment (Colab) Basic Syntax  Syntax refers to the spelling and grammar of a programming language.  Each program defines its own syntactical rules that control which words the computer understands, which combinations of words are meaningful, and what punctuation is necessary.  Text-based programming languages are based on sequences of characters,  Visual programming languages are based on the spatial layout and connections between symbols (which may be textual or graphical).  Syntax differences between programming languages  Syntax errors and error checking in IDE Basic Syntax  Number and String (characters)  Keywords  Python Variables  Single and multiple statement  Comments  Functions  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. Numbers There are three numeric types in python Integer (int): Non-decimal whole numbers e.g. 1,3,191 e.t.c. Decimal (Float): Decimal numbers e.g. 1.1, 3.4, e.t.c. Complex: Complex numbers e.g. 3j, 2j, e.t.c. String (Literals)  String literals in python are surrounded by either single quotation marks, or double quotation marks 'hello' is the same as "hello"  Strings are Arrays. An array is collection of elements, each identified by at least one array index or key. a = “Hello” a - “e”  Escape Character: To insert characters that are illegal in a string, use an escape character. Python code: “My name is spell \“Messi\“ not \“Mercy\". “ Output: My name is “Messi” not “Mercy” Variable  A variable is defined as a place in computer memory where you can store values temporarily and use them later, represented as a symbol or name associated with a value and whose associated value may be changed.  Variables can represent numeric values, characters, character strings, or memory addresses.  Rather than entering data directly into a program, a programmer can use variables to represent the data. Then, when the program is executed, the variables are replaced with real data. This makes it possible for the same program to process different sets of data.  The name given to variable is know as Identifier. Rules for Python variables  A variable name must start with a letter or the underscore character  A variable name cannot start with a number  A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )  Variable names are case-sensitive (age, Age and AGE are three different variables) Comments  Inline comment #  Block comment ‘’’ ‘’’ Python Keywords  Keywords are the reserved words (can not be used as a identifier) in Python  They are case sensitive Operations (Assignment)  Operators are used to perform operations on variables and values.  Python divides the operators in the following groups:  Arithmetic operators  Assignment operators  Comparison operators  Logical operators  Identity operators  Membership operators  Bitwise operators Arithmetic Operators Arithmetic Operators Arithmetic Operators  Value Value  Value Variable  Variable Value  Variable Variable Assignment Operators Assignment Operators Assignment Operators  Variable Value  Variable Variable Comparison Operators Comparison Operators Comparison Operators  Value Value  Value Variable  Variable Value  Variable Variable Logical Operators Logical Operators Formula And OR True and True = True True or True = True False and True = False False or True = True True and False = False True or False = True False and False = False False or False = False Decision Making : Conditions and IF Statements  Python supports the usual logical conditions from mathematics:  Equals: a == b  Not Equals: a != b  Less than: a < b  Less than or equal to: a b  Greater than or equal to: a >= b  An "if statement" is written by using the if keyword.  If  If… Elif  If… else  If… elif… else  If statement: a = 33 b = 200 IF Statement if b > a: print("b is greater than a")  elif statement: a = 33 b = 33 ELIF Statement if b > a: (Else If) print("b is greater than a") elif a == b: print("a and b are equal") IF… ELIF … ELSE Statement  else statement: a = 200 b = 33 if b > a: print("b is greater than a") elif a == b: print("a and b are equal") else: print("a is greater than b") Loops Python has two primitive loop commands: while loops for loops While Loops  With the while loop we can execute a set of statements as long as a condition is true. i=1 while i < 6: print(i) i += 1 Learn Python Online https://www.w3schools.com/python/default.asp Example a = 400 b = 200 if b > a: print("b is greater than a") print("Extra info") #stops here elif a > b: print("b is less than a") print("Extra info") #stops here elif a == b: print("a is equal to b") print("Extra info") #stops here else: print("Unknown Exception") Example  a=5  b=8  c=9  if a + b == c:  print('a + b is greater than c')  else:  print('Unhandled exception') Exercise  What is the difference between Variable Declaration and Variable Definition? Learn Python Online https://www.w3schools.com/python/default.asp

Use Quizgecko on...
Browser
Browser