




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
• 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
Typology: Study notes
1 / 232
This page cannot be seen from the preview
Don't miss anything!





























































































UNIT I
1
INTRODUCTION
Unit Structure 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
1.0 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.
1.1 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.
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.
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.
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.
1.4 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 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.
1.5 RUNNING PYTHON PROGRAM:
There are different ways of working in Python:
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.
To execute the Python file, you have to use the keyword ‗Python‘ followed by the file name with extension.py See the example given in the screen above with the output of the file.
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.
Create a Python file with .py extension and open it with the Python shell. The file looks like the image given below.
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.
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.
1.6 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
1.6.4 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.
1.7 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.
1.8 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]
L
[10,20,30]
Brackets can be used for indexing and lookup of elements
Example:
L1[1] = 40
L
[10,40,30]
Example: To lookup the element of list L
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)
<class ‗set‘>
Example:
Create a dictionary with two elements with keys, ‗rollno‘ and ‗name‘
d1= {‗rollno‘:101, ‗name‘:‘ Vivek‘}
type(d1)
<class ‗dict‘>
Brackets can be used to access the value of dictionary element by specifying the key.
d1[‗rollno‘]
101
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.
2
VARIABLES AND EXPRESSION
2.0 OBJECTIVES
2.1 INTRODUCTION
If you give a variable an illegal name, you get a syntax error:
2.3 TYPE CONVERSION