Python_Lecture_1(introduction)
Python_Lecture_1(introduction)
INTRODUCTION
Unit 1: Introduction to Python and installation
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.
Course Objectives
• Establish a broad background in Python programming.
• Learn how to read documentation and quickly get new tools up and running
• 1.2 History
• 1.3 Features
• 1.6 Debugging
• 1.9 Summary
• 1.10 References
Lecture Objectives
After reading through this chapter, you will be able to :
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
• Python makes the development and debugging fast because there is no compilation step
• Downloads: http://www.python.org
• Documentation: http://www.python.org/doc/
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.
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.
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.
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
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.
• Web development
• Everyday tasks
• Visual Programming
Why Python?
• Python is commonly used for developing websites and software, task
• Python has been adopted by many non-programmers such as accountants and scientists, for
• 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 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
• 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.
semicolons.
INSTALLING PYTHON
• To install Python, firstly download the Python distribution from official website of
• 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
Python code.
• 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.
• 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
• 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.
program of Python.
• Press the enter button to execute the print code of Python. The output gives the text “Hello World!” after you press
• Type any code of Python you want to execute and run directly on interactive mode.
• Another useful method of executing the Python code. Use the Python IDLE
• 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.
• Another useful method of executing the Python code. Use the Python IDLE
• 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 code, you have to open the ‘run’ menu and press the ‘Run Module’ option.
file and execute the Python code using this simple method using Python IDLE.
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.
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
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
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.
• 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
• 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’
• 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
• 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
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
• In this chapter we are more focused on types of errors in python like syntax error, runtime
• Execution of Python Program Using Command Prompt and Interactive Mode to Execute
Python Program.
REFERENCES
1) www.journaldev.com
2) www.edureka.com
3) www.tutorialdeep.com
4) www.xspdf.com
6) Python Programming for Beginners By Prof. Rahul E. Borate, Dr. Sunil Khilari, Prof.
Rahul S. Navale.
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.