0% found this document useful (0 votes)
10 views14 pages

Python Unit 1

Python is a high-level, interpreted, interactive, and object-oriented programming language designed for readability and ease of use, making it suitable for beginners. Developed by Guido van Rossum in the late 1980s, Python has evolved through several versions, with significant features such as dynamic typing, cross-platform compatibility, and a broad standard library. The document also outlines installation steps for various operating systems and explains the Python interpreter's functionality and execution process.

Uploaded by

chetali kumawat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views14 pages

Python Unit 1

Python is a high-level, interpreted, interactive, and object-oriented programming language designed for readability and ease of use, making it suitable for beginners. Developed by Guido van Rossum in the late 1980s, Python has evolved through several versions, with significant features such as dynamic typing, cross-platform compatibility, and a broad standard library. The document also outlines installation steps for various operating systems and explains the Python interpreter's functionality and execution process.

Uploaded by

chetali kumawat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Definition:

Python is a high-level, interpreted, interactive and object-oriented scripting language.


Python is designed to be highly readable. It uses English keywords frequently where as other
languages use punctuation, and it has fewer syntactical constructions than other languages.
 Python is Interpreted: Python is processed at runtime by the interpreter. You do not
need to compile your program before executing it. This is similar to PERL and PHP.
 Python is Interactive: You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.
 Python is Object-Oriented: Python supports Object-Oriented style or technique of
programming that encapsulates code within objects.
 Python is a Beginner's Language: Python is a great language for the beginner-level
programmers and supports the development of a wide range of applications from simple
text processing to WWW browsers to games.
History of Python
 Python was developed by Guido van Rossum in the late eighties
and early nineties at the National Research Institute for
Mathematics and Computer Science in the Netherlands.
 Python is derived from many other languages, including ABC,
Modula-3, C, C++, Algol-68, SmallTalk, Unix shell, and other
scripting languages.
 At the time when he began implementing Python, Guido van Rossum was also reading
the published scripts from "Monty Python's Flying Circus" (a BBC comedy series from
the seventies, in the unlikely case you didn't know). It occurred to him that he needed a
name that was short, unique, and slightly mysterious, so he decided to call the language
Python.
 Python is now maintained by a core development team at the institute, although Guido
van Rossum still holds a vital role in directing its progress.
 Python 1.0 was released on 20 February, 1991.
 Python 2.0 was released on 16 October 2000 and had many major new features,
including a cycle detecting garbage collector and support for Unicode. With this release
the development process was changed and became more transparent and community-
backed.
 Python 3.0 (which early in its development was commonly referred to as Python 3000 or
py3k), a major, backwards-incompatible release, was released on 3 December 2008 after
a long period of testing. Many of its major features have been back ported to the
backwards-compatible Python 2.6.x and 2.7.x version series.
 In January 2017 Google announced work on a Python 2.7 to go transcompiler, which The
Register speculated was in response to Python 2.7's planned end-of-life.
Python Features:
Python's features include:
 Easy-to-learn: Python has few keywords, simple structure, and a clearly defined syntax.
This allows the student to pick up the language quickly.
 Easy-to-read: Python code is more clearly defined and visible to the eyes.
 Easy-to-maintain: Python's source code is fairly easy-to-maintain.
 A broad standard library: Python's bulk of the library is very portable and cross-
platform compatible on UNIX, Windows, and Macintosh.
 Interactive Mode: Python has support for an interactive mode which allows interactive
testing and debugging of snippets of code.
 Portable: Python can run on a wide variety of hardware platforms and has the same
interface on all platforms.
 Extendable: You can add low-level modules to the Python interpreter. These modules
enable programmers to add to or customize their tools to be more efficient.
 Databases: Python provides interfaces to all major commercial databases.
 GUI Programming: Python supports GUI applications that can be created and ported to
many system calls, libraries, and windows systems, such as Windows MFC, Macintosh,
and the X Window system of UNIX.
 Scalable: Python provides a better structure and support for large programs than shell
scripting.

Need of Python Programming


 Software quality
 Developer productivity
 Support libraries
 Component integration
 Enjoyment
 It's Object-Oriented
 It's Free
 It's Portable
 It's Powerful
 Automatic memory management
 Programming-in-the-large support
 It's Mixable
 It's Easy to Use
 It's Easy to Learn
 Internet Scripting
 DatabaseProgramming
 Image Processing, AI, Distributed Objects, Etc.
Byte code Compilation:
Python first compiles your source code (the statements in your file) into a format
known as byte code. Compilation is simply a translation step, and byte code is a lower-
level, platform independent representation of your source code. Roughly, Python translates
each of your source statements into a group of byte code instructions by decomposing them
into individual steps. This byte code translation is performed to speed execution —byte code
can be run much more quickly than the original source code statements in your text file.

The Python Virtual Machine:


Once your program has been compiled to byte code (or the byte code has been loaded
from existing .pyc file), it is shipped off for execution to something generally known as the
python virtual machine (PVM).

Applications of Python:
1. Systems Programming
2. GUIs
3. Internet Scripting
4. Component Integration
5. Database Programming
6. Rapid Prototyping
7. Numeric and Scientific Programming

What Are Python’s Technical Strengths?


1. It‘s Object-Oriented and Functional
2. It‘s Free
3. It‘s Portable
4. It‘s Powerful
5. It‘s Mixable
6. It‘s Relatively Easy to Use
7. It‘s Relatively Easy to Learn
Download and installation Python software:
Step 1: Go to website [Link] and click downloads select version which you want.

Step 2: Click on Python 2.7.13 and download. After download open the file.
Step 3: Click on Next to continue.

Step 4: After installation location will be displayed. The Default location is C:\Python27.
Click on next to continue.
Step 5: After the python interpreter and libraries are displayed for installation. Click on Next
to continue.

Step 6: The installation has been processed.


Step 7: Click the Finish to complete the installation.

Setting up PATH to python:


 Programs and other executable files can be in many directories, so operating systems
provide a search path that lists the directories that the OS searches for executables.
 The path is stored in an environment variable, which is a named string maintained by the
operating system. This variable contains information available to the command shell and
other programs.
 Copy the Python installation location C:\Python27
 Right-click the My Computer icon on your desktop and choose Properties. And then
select Advanced System properties.

 Goto Environment Variables and go to System Variables select Path and click on
Edit.

 Add semicolon (;) at end and copy the location C:\Python27 and give semicolon (;) and
click OK.
Running Python:
a. Running Python Interpreter:
Python comes with an interactive interpreter. When you type python in your shell or
command prompt, the python interpreter becomes active with a >>> prompt and waits for
your commands.

Now you can type any valid python expression at the prompt. Python reads the typed
expression, evaluates it and prints the result.

b. Running Python Scripts in IDLE:


 Goto File menu click on New File (CTRL+N) and write the code and save [Link]
a=input("Enter a value ")
b=input("Enter b value ")
c=a+b
print "The sum is",c
 And run the program by pressing F5 or RunRun Module.
c. Running python scripts in Command Prompt:
 Before going to run we have to check the PATH in environment variables.
 Open your text editor, type the following text and save it as [Link].
print "hello"
 And run this program by calling python [Link]. Make sure you change to the directory
where you saved the file before doing it.
Python's strengths, such as interpreted nature and dynamically-typed and cross-platform compatibility, .
⬛ 1. Interpreted Nature
 Python is an interpreted language, which means the code is executed line by line by an
interpreter (such as CPython). 

 You don’t need to compile Python code into machine language before running it, unlike C or
C++.

 Advantages: 

o Easier debugging because errors are shown immediately when encountered.

o Fast development cycle – you can write and run code without a separate compilation
step.

 Example: 

 print("Hello") 

You can run this immediately without compiling.

⬛ 2. Dynamically Typed
 Python is dynamically typed, which means you don’t have to declare the data type of a
variable explicitly. 

 The type is determined at runtime based on the value assigned.

 Advantages: 

o Less code and faster development.

o More flexibility in using variables.

 Example: 

 x = 10 # x is an integer

 x = "Hello" # Now x becomes a string

No error occurs because Python determines the type dynamically.

⬛ 3. Cross-Platform Compatibility
 Python is cross-platform, meaning the same Python code can run on different operating
systems (Windows, Linux, macOS) without modification. 

 Why?
Because Python is available for all major platforms, and the interpreter handles system-level
differences.

 Example: 

o A script written on Windows will also run on Linux or macOS if Python is installed.

⬛ Summary in Simple Terms


 Interpreted → Runs line by line without compilation.

 Dynamically Typed → No need to declare variable types; they change at runtime. 


 Cross-Platform → Write once, run anywhere (with Python installed).

installing Python on different operating systems (Windows, macOS, and Linux), understand the Python
interpreter and its usage. Explain this

⬛ 1. Installing Python on Different Operating Systems


Python is available for Windows, macOS, and Linux. The installation steps vary slightly:

a) On Windows
1. Go to the official Python website: [Link]

2. Download the latest version for Windows (e.g., Python 3.x).

3. Run the installer:

o ⬛
Check "Add Python to PATH" during installation.

o ⬛
Click Install Now.

4. Verify installation:

5. python --version

or
python3 --version

b) On macOS
1. macOS often comes with Python pre-installed (usually Python 2.x), but you can install the latest
version.

2. Methods to install:

o Using official installer:

 Download from [Link].

 Install like any other macOS app.

o Using Homebrew (recommended):

o brew install python

3. Verify:

4. python3 --version

c) On Linux
Most Linux distributions come with Python pre-installed.
To install or upgrade:
 Ubuntu/Debian:

 sudo apt update

 sudo apt install python3

 Fedora:

 sudo dnf install python3


Verify:
python3 --version

⬛ 2. Understanding the Python Interpreter and Its Usage


The Python interpreter is the program that reads and executes Python code line by line.
Ways to Use the Python Interpreter
1. Interactive Mode (REPL)

o Just type python or python3 in the terminal or command prompt.

o You get a prompt like:

o >>> print("Hello")

o Hello

o Good for quick testing.

2. Script Mode

o Save your code in a file with .py extension (e.g., [Link]).

o Run it:

o python [Link]

3. Using IDEs

o You can use IDLE, PyCharm, VS Code, Jupyter Notebook, etc.

What Does the Interpreter Do?


 Reads your Python code.

 Converts it into bytecode.

 Executes it using the Python Virtual Machine (PVM).


Summary:
 Installation → Simple for all OS (download or use package manager).

 Interpreter → Lets you run Python code in interactive or script mode.

⬛ Difference Between Python Interpreter and Compiler


Feature Interpreter Compiler
Execution Converts the entire code into machine language
Executes line by line
Method before running
Error Handling Stops at the first error Shows all errors after compilation
Slower (due to line-by-line
Speed Faster (runs precompiled code)
execution)
Immediate execution (good for
Output Creates an executable file
testing)
Example Python, JavaScript C, C++
⬛ Python uses an interpreter, which means:
 Code is not compiled into machine code ahead of time.
 It is converted to bytecode and then executed by the Python Virtual Machine (PVM).

⬛ How Python Code Executes Internally (Step-by-Step)


Here’s the process:
1. You write Python code (example: print("Hello")).

2. The Python Interpreter reads the code.

3. It converts the code into bytecode (.pyc files in some cases).

4. The Python Virtual Machine (PVM) executes the bytecode.

5. The result is displayed on the screen.

Diagram:
Python Source Code (.py)

Python Interpreter

Bytecode (.pyc)

Python Virtual Machine (PVM)

Output


Key Point:
Python is not purely interpreted; it uses a hybrid approach (interpretation + bytecode execution)

**********************************************************************************
Unit -1
Introduction to Python: History of Python, highlight Python's strengths, such as interpreted
nature and dynamically-typed and cross-platform compatibility, installing Python on different
operating systems (Windows, macOS, and Linux), understand the Python interpreter and its usage

***********************************************************************************

You might also like