0% found this document useful (0 votes)
16 views42 pages

Python_Lecture_1(introduction)

Uploaded by

egilhani981
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
16 views42 pages

Python_Lecture_1(introduction)

Uploaded by

egilhani981
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 42

PYTHON PROGRAMMING

INTRODUCTION
Unit 1: Introduction to Python and installation

Python Programming : Lecture One


Dr. Khalil Alawdi 2

Course Description

This course is framed to cover Python Basics like Data types, Lists,
tuples, Dictionaries, Loops, functions in detail. Learn to use Object
Oriented Programming with classes. Besides that, it will covers
advanced level like Python for Machine Learning, AI, Web
development and Data Science.

Python Programming : Lecture One


Dr. Khalil Alawdi 3

Course Objectives
• Establish a broad background in Python programming.

• Prepare you for the inevitable coding interview

• Survey popular tools in academia/industry for data analysis and exploration

• Learn how to read documentation and quickly get new tools up and running

• Learn basic distributed computing frameworks

Python Programming : Lecture One


Dr. Khalil Alawdi 4

Books & References


Sr. No. Title Author / s Publisher Edition Year
1 Think Python Allen Downey O’Reilly 1 2012
2 An Introduction to Computer Thomas Erl, Zaigham SPD 1 2014
Science using Python Mahmood, and Ricardo
3 Python GUI Programming Burkhard A. Meier Packt 1 2015
Cookbook
4 Introduction to Problem Solving E. Balagurusamy TMH 1 2015
with Python
5 Murach’s Python programming Joel Murach, Michael SPD 1 2017
Urban
6 Object-oriented Programming in Michael H. Goldwasser, Pearson 1 2008
Python David Letscher Prentice
Hall
7 Exploring Python Budd TMH 1 2016

Python Programming : Lecture One


Dr. Khalil Alawdi 5

Outline of this lecture


• 1.0 Objectives

• 1.1 Introduction: The Python Programming Language

• 1.2 History

• 1.3 Features

• 1.4 Installing Python

• 1.5 Running Python program

• 1.6 Debugging

• 1.6.1 Syntax Errors

• 1.6.2 Runtime Errors

• 1.6.3 Semantic Errors

• 1.6.4 Experimental Debugging

• 1.7 Formal and Natural Languages

• 1.8 The Difference Between Brackets, Braces, and Parentheses

• 1.9 Summary

• 1.10 References

• 1.11 Unit End Exercise

Python Programming : Lecture One


Dr. Khalil Alawdi 6

Lecture Objectives
After reading through this chapter, you will be able to :

• To understand and use the basic of python.

• To understand the history and features of python programming.

• To understand the installation of python.

• To handle the basis errors in python.

• To understand the difference between brackets, braces and parenthesis.

Python Programming : Lecture One


Dr. Khalil Alawdi 7

INTRODUCTION: THE PYTHON PROGRAMMING LANGUAGE


• Python is an object-oriented, high level language, interpreted, dynamic and multipurpose

programming language.

• Python is not intended to work on special area such as web programming. That is why it is

known as multipurpose because it can be used with web, enterprise, 3D CAD etc.

• We don’t need to use data types to declare variable because it is dynamically typed so we

can write a=10 to declare an integer value in a variable.

• Python makes the development and debugging fast because there is no compilation step

included in python development.

Python Programming : Lecture One


Dr. Khalil Alawdi 8

INTRODUCTION: THE PYTHON PROGRAMMING LANGUAGE

• Easy to interface with C/ObjC/Java/Fortran

• Easy-ish (Quite easy) to interface with C++ (via SWIG)

• Great interactive environment

• Downloads: http://www.python.org

• Documentation: http://www.python.org/doc/

• Free book: http://www.diveintopython.org

Python Programming : Lecture One


Dr. Khalil Alawdi 9

HISTORY
• Python was first introduced by Guido Van Rossum in 1991 at the National Research Institute for
Mathematics and Computer Science, Netherlands.
• Though the language was introduced in 1991, the development began in the 1980s. Previously
van Rossum worked on the ABC language at Centrum Wiskunde & Informatica (CWI) in the
Netherlands.
• The ABC language was capable of exception handling and interfacing with the Amoeba operating
system. Inspired(‫ )يوحي يسبب‬by the language, Van Rossum first tried out making his own version of
it.
• Python is influenced(‫ )يؤثر‬by programming languages like: ABC language, Modula-3, Python is
used for software development at companies and organizations such as Google, Yahoo, CERN,
Industrial Light and Magic, and NASA.
• Why the Name Python?
• Python developer, Rossum always wanted the name of his new language to be short, unique, and
mysterious(‫ )سري مبهم غامض‬.
• Inspired by Monty Python’s Flying Circus, a BBC comedy series, he named it Python.

Python Programming : Lecture One


Dr. Khalil Alawdi 10

FEATURES
There are a lot of features provided by python programming language as follows:
1. Easy to Code:
• Python is a very developer-friendly language which means that anyone and everyone can learn to code it in a
couple of hours or days.
• As compared to other object-oriented programming languages like Java, C, C++, and C#, Python is one of the
easiest to learn.

2. Open Source and Free:


• Python is an open-source programming language which means that anyone can create and contribute to its
development.
• Python has an online forum where thousands of coders gather daily to improve this language further. Along with
this Python is free to download and use in any operating system, be it Windows, Mac or Linux.

Python Programming : Lecture One


Dr. Khalil Alawdi 11

FEATURES
3. Support for GUI:
• GUI or Graphical User Interface is one of the key aspects of any programming language because it has the
ability to add flair to code and make the results more visual.
• Python has support for a wide array of GUIs which can easily be imported to the interpreter, thus making this
one of the most favorite languages for developers.

4. Object-Oriented Approach:
• One of the key aspects of Python is its object-oriented approach. This basically means that Python
recognizes the concept of class and object encapsulation thus allowing programs to be efficient in the long
run.

Python Programming : Lecture One


Dr. Khalil Alawdi 12

FEATURES
5. Highly Portable:
• Suppose you are running Python on Windows and you need to shift the same to either a Mac or a Linux system,
then you can easily achieve the same in Python without having to worry about changing the code.
• This is not possible in other programming languages, thus making Python one of the most portable languages
available in the industry.

6. Highly Dynamic
• Python is one of the most dynamic languages available in the industry today. What this basically means is
that the type of a variable is decided at the run time and not in advance.
• Due to the presence of this feature, we do not need to specify the type of the variable during coding, thus
saving time and increasing efficiency

Python Programming : Lecture One


Dr. Khalil Alawdi 13

FEATURES
7. Large Standard Library:
• Out of the box, Python comes inbuilt with a large number of libraries that can be imported at any instance and be
used in a specific program.
• The presence of libraries also makes sure that you don’t need to write all the code yourself and can import the
same from those that already exist in the libraries.

8. Databases:
• Python provides interfaces to all major commercial databases.

9. Scalable:
• Python provides a better structure and support for large programs than shell scripting.

Python Programming : Lecture One


Dr. Khalil Alawdi 14

What can you do with Python?


You can do some things include:
• Data analysis and machine learning

• Web development

• Automation(‫ )التشغيل اآللي‬or scripting

• Software testing and prototyping

• Everyday tasks

• Visual Programming

Python Programming : Lecture One


Dr. Khalil Alawdi 15

Why Python?
• Python is commonly used for developing websites and software, task

automation, data analysis, and data visualization.

• Since it's relatively easy to learn:

• Python has been adopted by many non-programmers such as accountants and scientists, for

a variety of everyday tasks, like organizing finances.

Python Programming : Lecture One


Dr. Khalil Alawdi 16

Why Python so popular?


• Python is popular for several reasons. Here's a deeper look at what makes it versatile and easy for coders to use.

• It has a simple syntax that mimics(‫محاكي‬ ‫ )مقلد‬natural language, so it’s easier to read and understand. This makes it quicker
to build projects and faster to improve on them.

• It’s versatile (multiple usages) . Python can be used for many different tasks, from web development to machine learning.

• It’s beginner friendly, making it popular for entry-level coders.

• It’s open source, which means it’s free to use and distribute, even for commercial purposes.

• Python’s archive of modules and libraries—bundles of code that third-party users have created to expand Python’s

capabilities—is vast and growing.

• Python has a large and active community that contributes to Python’s pool of modules and libraries and acts as a helpful

resource for other programmers. The vast support community means that when coders need help, finding a solution is
relatively easy; somebody has likely encountered the same problem before.

Python Programming : Lecture One


Dr. Khalil Alawdi 17

Python Syntax vs other programming languages?

• Python's clean and readable syntax is one of its outstanding features.

• The language prioritizes code readability by using indentation to

define blocks of code, making it highly accessible, especially for


beginners and non-programmers.

• In contrast, languages like C and Java rely on curly braces and

semicolons.

Python Programming : Lecture One


Dr. Khalil Alawdi 18

INSTALLING PYTHON
• To install Python, firstly download the Python distribution from official website of

python (www.python.org/ download).

• Having downloaded the Python distribution now execute it.

• Setting Path in Python

• Before starting working with Python, a specific path is to set path follow the steps:

• Right click on My Computer--> Properties -->Advanced System setting -->Environment Variable -->New

• In Variable name write path and in Variable value copy path up to C:// Python (i.e., path where Python is

installed). Click Ok ->Ok.

Python Programming : Lecture One


Dr. Khalil Alawdi 19

RUNNING PYTHON PROGRAM


There are different ways of working in Python:
• (1) How to Execute Python Program Using Command Prompt:

• If you want to create a Python file in .py extension and run.

• You can use the Windows command prompt to execute the

Python code.

Python Programming : Lecture One


Dr. Khalil Alawdi 20

RUNNING PYTHON PROGRAM


Example:
Here is the simple code of Python given in the Python file demo.py. It contains only single line code of
Python which prints the text “Hello World!” on execution.

• So, how you can execute the Python program using the command prompt. To see this, you have to

first open the command prompt using the ‘window+r’ keyboard shortcut. Now, type the word ‘cmd’
to open the command prompt.

• This opens the command prompt with the screen as given below. Change the directory location to

the location where you have just saved your Python .py extension file.

• We can use the cmd command ‘cd’ to change the directory location. Use ‘cd..’ to come out of

directory and “cd” to come inside of the directory. Get the file location where you saved your
Python file.

Python Programming : Lecture One


Dr. Khalil Alawdi 21

RUNNING PYTHON PROGRAM

• See the example To execute the Python file, you have to use the keyword ‘Python’ followed

by the file name with extension.py given in the screen above with the output of the file

Python Programming : Lecture One


Dr. Khalil Alawdi 22

RUNNING PYTHON PROGRAM


• (2) Interactive Mode to Execute Python Program:

• To execute the code directly in the interactive mode. You have to open the

interactive mode. Press the window button and type the text “Python”.
Click the “Python 3.7(32 bit) Desktop app” as given below to open the
interactive mode of Python.

Python Programming : Lecture One


Dr. Khalil Alawdi 23

RUNNING PYTHON PROGRAM

Python Programming : Lecture One


Dr. Khalil Alawdi 24

RUNNING PYTHON PROGRAM


• You can type the Python code directly in the Python interactive mode. Here, in the image below contains the print

program of Python.
• Press the enter button to execute the print code of Python. The output gives the text “Hello World!” after you press

the enter button.

• Type any code of Python you want to execute and run directly on interactive mode.

Python Programming : Lecture One


Dr. Khalil Alawdi 25

RUNNING PYTHON PROGRAM


• (3) Using IDLE (Python GUI) to Execute Python Program:

• Another useful method of executing the Python code. Use the Python IDLE

GUI Shell to execute the Python program on Windows system.

• Open the Python IDLE shell by pressing the window button of the

keyboard. Type “Python” and click the “IDLE (Python 3.7 32 bit)” to open
the Python shell.

Python Programming : Lecture One


Dr. Khalil Alawdi 26

RUNNING PYTHON PROGRAM


• (3) Using IDLE (Python GUI) to Execute Python Program:

• Another useful method of executing the Python code. Use the Python IDLE

GUI Shell to execute the Python program on Windows system.

• Open the Python IDLE shell by pressing the window button of the

keyboard. Type “Python” and click the “IDLE (Python 3.7 32 bit)” to open
the Python shell.

Python Programming : Lecture One


Dr. Khalil Alawdi 27

RUNNING PYTHON PROGRAM

Python Programming : Lecture One


Dr. Khalil Alawdi 28

RUNNING PYTHON PROGRAM


• Create a Python file with .py extension and open it with the Python shell. The file looks like the

image given below.

Python Programming : Lecture One


Dr. Khalil Alawdi 29

RUNNING PYTHON PROGRAM


• It contains the simple Python code which prints the text “Hello World!”. In order to execute the

Python code, you have to open the ‘run’ menu and press the ‘Run Module’ option.

Python Programming : Lecture One


Dr. Khalil Alawdi 30

RUNNING PYTHON PROGRAM


• A new shell window will open which contains the output of the Python code. Create your own

file and execute the Python code using this simple method using Python IDLE.

Python Programming : Lecture One


Dr. Khalil Alawdi 31

DEBUGGING
• Debugging means the complete control over the program execution.
Developers use debugging to overcome program from any bad issues.
• Debugging is a healthier process for the program and keeps the diseases
bugs far away.
• Python also allows developers to debug the programs using pdb module
that comes with standard Python by default.
• We just need to import pdb module in the Python script. Using pdb module,
we can set breakpoints in the program to check the current status.
• We can Change the flow of execution by using jump, continue statements.

Python Programming : Lecture One


Dr. Khalil Alawdi 32

DEBUGGING
• Syntax Error:
• Errors are the mistakes or faults performed by the user which results in abnormal working of the
program.
• However, we cannot detect programming errors before the compilation of programs. The
process of removing errors from a program is called Debugging.
• A syntax error occurs when we do not use properly defined syntax in any programming
language. For example: incorrect arguments, indentation, use of undefined variables etc.
• Example:

• age=16
• if age>18: print ("you can vote”) # syntax error because of not using indentation
• else print ("you cannot vote”) #syntax error because of not using indentation

Python Programming : Lecture One


Dr. Khalil Alawdi 33

DEBUGGING
• Runtime Errors:

• The second type of error is a runtime error, so called because the error does not appear until
after the program has started running.
• These errors are also called exceptions because they usually indicate that something
exceptional (and bad) has happened.
• Some examples of Python runtime errors:
• division by zero
• performing an operation on incompatible types
• using an identifier which has not been defined
• accessing a list element, dictionary value or object attribute which doesn’t exist
• trying to access a file which doesn’t exist

Python Programming : Lecture One


Dr. Khalil Alawdi 34

DEBUGGING
• Semantic Errors:
• The third type of error is the semantic error. If there is a semantic error in
your program, it will run successfully in the sense that the computer will not
generate any error messages, but it will not do the right thing. It will do
something else.
• The problem is that the program you wrote is not the program you wanted to
write. The meaning of the program (its semantics) is wrong.
• Identifying semantic errors can be tricky because it requires you to work
backward by looking at the output of the program and trying to figure out
what it is doing.
Python Programming : Lecture One
Dr. Khalil Alawdi 35

DEBUGGING
• Experimental Debugging:

• One of the most important skills you will acquire is debugging. Although it can be frustrating,
debugging is one of the most intellectually rich, challenging, and interesting parts of
programming.
• Debugging is also like an experimental science. Once you have an idea about what is going
wrong, you modify your program and try again.
• For some people, programming and debugging are the same thing. That is, programming is
the process of gradually debugging a program until it does what you want.
• The idea is that you should start with a program that does something and make small
modifications, debugging them as you go, so that you always have a working program.

Python Programming : Lecture One


Dr. Khalil Alawdi 36

FORMAL AND NATURAL LANGUAGES


• Natural languages are the languages people speak, such as English, Spanish, and French. They were not designed by
people (although people try to impose some order on them); they evolved naturally.

• Formal languages are languages that are designed by people for specific applications.

• For example, the notation that mathematicians use is a formal language that is particularly good at denoting
relationships among numbers and symbols. Chemists use a formal language to represent the chemical structure of
molecules.

• Programming languages are formal languages that have been designed to express computations.

• Formal languages tend to have strict rules about syntax. For example, 3 + 3 = 6 is a syntactically correct mathematical
statement.

• Syntax rules come in two flavors, pertaining to tokens and structure. Tokens are the basic elements of the language,
such as words, numbers, and chemical elements.

• The second type of syntax rule pertains to the structure of a statement; that is, the way the tokens are arranged. The
statement 3+ = 3 is illegal because even though + and = are legal tokens, you can’t have one right after the other.
Python Programming : Lecture One
Dr. Khalil Alawdi 37

THE DIFFERENCE BETWEEN BRACKETS, BRACES,


AND PARENTHESES
• Brackets [ ]:
• Brackets are used to define mutable data types such as list or list comprehensions.
• Example:
• To define a list with name as L1 with three elements 10,20 and 30 >>> L1 = [10,20,30] >>> L1 [10,20,30]

• Brackets can be used for indexing and lookup of elements Example: >>>L1[1] = 40 >>>L1 [10,40,30]
• Example:
• To lookup the element of list L1 >>> L1[0] 10

• Brackets can be used to access the individual characters of a string or to make string slicing
• Example:
• Lookup the first characters of string str>>>’mumbai’ >>> str [0] ‘m’
• Example:
• To slice a string >>> str [1:4] ‘umb’

Python Programming : Lecture One


Dr. Khalil Alawdi 38

THE DIFFERENCE BETWEEN BRACKETS, BRACES,


AND PARENTHESES

• Braces {}
• Curly braces are used in python to define set or dictionary.
• Example:
• Create a set with three elements 10,20,30. >>> s1 = {10,20,30} >>> type(s1)
• Example:
• Create a dictionary with two elements with keys, ‘rollno’ and ‘name’ >>> d1= {‘rollno’:101, ‘name’:’ Vivek’} >>> type(d1)

• Brackets can be used to access the value of dictionary element by specifying the key. >>>
d1[‘rollno’] 101

Python Programming : Lecture One


Dr. Khalil Alawdi 39

THE DIFFERENCE BETWEEN BRACKETS, BRACES,


AND PARENTHESES

• Parentheses ( )
• Parentheses can be used to create immutable sequence data type tuple.
• Example:
• Create a tuple named ‘t1’ with elements 10,20,30 >>> t1= (10,20,30) >>> type(t1)
• Parentheses can be used to define the parameters of function definition and function call.
• Example:
• Multiply two numbers using a function def mul(a,b): returns a*b x=2 y=3 z=mul (2,3) print(x,’*’,y,’=’z)

• In the function definition def mul(a,b) formal parameters are specified using parentheses

• In the function call z=mul (2,3) actual values are specified using parenthesis

Python Programming : Lecture One


Dr. Khalil Alawdi 40

SUMMARY
• In this chapter we studied Introduction, history, features of Python Programming Language.

• We don’t need to use data types to declare variable because it is dynamically typed so we

can write a=10 to declare an integer value in a variable.

• In this chapter we are more focused on types of errors in python like syntax error, runtime

error, semantic error and experimental debugging.

• Execution of Python Program Using Command Prompt and Interactive Mode to Execute

Python Program.

• Also, we studied Difference between Brackets, Braces, and Parentheses in python.

Python Programming : Lecture One


Dr. Khalil Alawdi 41

REFERENCES
1) www.journaldev.com

2) www.edureka.com

3) www.tutorialdeep.com

4) www.xspdf.com

5) Think Python by Allen Downey 1st edition.

6) Python Programming for Beginners By Prof. Rahul E. Borate, Dr. Sunil Khilari, Prof.

Rahul S. Navale.

Python Programming : Lecture One


Dr. Khalil Alawdi 42

Homework 1
• Use a web browser to go to the Python website http: // python.org. This page

contains information about Python and links to Python related pages, and it
gives you the ability to search the Python documentation.
• For example, if you enter print in the search window, the first link that appears is the

documentation of the print statement. At this point, not all of it will make sense to you, but it
is good to know where it is.

• Start the Python interpreter and type help () to start the online help utility. Or

you can type help ('print') to get information about the print statement.

Python Programming : Lecture One

You might also like