python 1st unit
python 1st unit
What is Python?
Python is an advanced, interpreted programming language known for its readability and
simplicity. It supports various programming paradigms, such as procedural, object-oriented,
and functional. Python's wide range of applications, from web development and app creation
to data analytics and artificial intelligence, underscore its usefulness. Its robust assortment of
libraries and frameworks contributes to its popularity among novices and professionals.
Python is a programming language that includes features of C and Java. It provides the style
of writing elegant code like C, and for object-oriented programming, it offers classes and
objects like Java.
In Python, the program to add two numbers will be as follows:
• At the National Research Institute for Mathematics and Computer Science in the
Netherlands, Guido van Rossum created Python in the late 1980s as an alternative to
the ABC language that could handle exceptions and interfaces.
• Python is derived from programming languages such as ABC, Modula 3, small talk,
and Algol-68.
• Van Rossum picked the name Python for the new language from a TV show, Monty
Python's Flying Circus.
• A Python page is a file with a .py extension that can contain a combination of HTML
tags and Python scripts.
• Python is an open-source scripting language.
• Python is open-source, meaning anyone can download it freely
from www.Python.org and use it to develop programs. Its source code can be
accessed and modified as required in the project.
• Python is one of the official languages used by the tech giant Google.
Features of Python
Python is gaining popularity in the programming community; there are many reasons behind
this.
Evolution of Python
The language was finally released in 1991. When it was released, it used a lot fewer codes
to express the concepts, when we compare it with Java, C++ & C. Its design philosophy
was quite good too. Its main objective is to provide code readability and advanced
developer productivity. When it was released, it had more than enough capability to provide
classes with inheritance, several core data types of exception handling and functions.
The first ever version of Python(i.e. Python 1.0) was introduced in 1991. Since its inception
and introduction of Version 1, the evolution of Python has reached up to Version 3.x (till 2017).
Here is the brief chart depicting the timeline of the release of different versions of Python
programming language.
Execution Modes in python
As we know that python uses an interpreter for the execution of source code. now, there are two
ways in which we can use the interpreter. They are as follows:
• Interactive Mode
• Script Mode
Interactive Mode
In this mode, we can execute a single statement at a time. Moreover, to use the interactive mode,
we have to write the statement in front of ‘>>>’ and press enter
This results in the output of that particular statement immediately. This mode is easy and
convenient to use to see the instant output. But, at the same time, we cannot save the whole code
and have to write it again and again to execute it.
Script Mode
In this mode we have to write the whole source code and save it as a Python source code file.
Furthermore, we can execute this file using the interpreter. Moreover, we save the python source
code file with the extension ‘.py’.
Python Identifiers:
All the variables, class, object, functions, lists, dictionaries etc. in Python are together termed
as Python Identifiers. Identifiers are the basis of any Python program. Almost every Python
Code uses some or other identifiers.
Reserved keywords:
Python has a set of keywords that are reserved words that cannot be used as variable names,
function names, or any other identifiers:
Keyword Description
as To create an alias
finally Used with exceptions, a block of code that will be executed no matter if there is an
or A logical operator
Python Indentation
Where in other programming languages the indentation in code is for readability only, the
indentation in Python is very important.
Explanation – It gives an invalid syntax error. Because a single quote after “it” is
considered the end of the string and rest part is not part of a string. It can be corrected as:
Output:
Python Comments
Creating a Comment
Comments starts with a #, and Python will ignore them:
Output
A comment does not have to be text that explains the code, it can also be used to prevent
Python from executing code:
Multiline comments:
The code above will print “Hello, World!” on one line, an empty line on the next, and then
“Goodbye, World!” on the following line.
Multiple Statement Groups as Suites
In Python, a suite is a block of code that consists of one or more statements. It is used to group
related statements together, and is typically used when defining functions, classes, and control
structures.
A suite is typically indented, with each line of the suite being indented the same amount. The
indentation is used to indicate that the statements in the suite belong together and should be
treated as a single block.
Here is an example of a suite in a function definition:
def greet(name):
greet("rakesh")
Output:
Running Python:
1st way:
Google colab:
1. Search for google colab
2. Register and sign in
3. Open new notebook
4. Type the code and click on debug button
2nd way:
Using Text Based:
If you find that you do not have Python installed on your computer, then you can download it
for free from the following website: https://www.python.org/
To Run the Python program in a command prompt
C:\Users\Your Name>python helloworld.py
Hello, World!
Using IDE:
C:\Users\Your Name>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, World!")
Hello, World!