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

Introduction to Python Programming

Python is a versatile, high-level programming language known for its simplicity and readability, making it ideal for beginners. It supports various programming paradigms, including object-oriented and procedural programming, and is widely used in fields such as data science, web development, and artificial intelligence. Python is open-source, cross-platform, and has a rich ecosystem of libraries and frameworks that enhance its capabilities.

Uploaded by

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

Introduction to Python Programming

Python is a versatile, high-level programming language known for its simplicity and readability, making it ideal for beginners. It supports various programming paradigms, including object-oriented and procedural programming, and is widely used in fields such as data science, web development, and artificial intelligence. Python is open-source, cross-platform, and has a rich ecosystem of libraries and frameworks that enhance its capabilities.

Uploaded by

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

What is Python

Python is a general-purpose, dynamic, high-level, and interpreted programming language. It supports Object
Oriented programming approach to develop applications. It is simple and easy to learn and provides lots of high-
level data structures.

 Python is a general-purpose programming language. That means it was designed and


developed to write software for a wide variety of disciplines. Python has been used to
write applications to solve problems in biology, chemistry, financial analysis, numerical
analysis, robotics, and many other fields.

Why Python is a High-Level Language


Python is considered a high-level programming language due to its abstraction from the hardware and its user-friendly features. Here are the key reasons:
 Human-Readable Syntax: Python's syntax is simple and resembles natural language, making it easier for developers to write and understand code.
 Built-in Libraries and Functions: Python provides extensive libraries and built-in functions for tasks like web development, data analysis, artificial
intelligence, and more, reducing the need for manual implementation.
 Portability: Python code can run on different platforms (Windows, macOS, Linux) without modification, thanks to its high-level nature.
 Automatic Memory Management: Python handles memory allocation and garbage collection automatically, freeing developers from these low-level
concerns.

Python History and Versions


Python was started in December 1989 by Guido Van Rossum at CWI in [Link] appeared in the
market in February 1991. Python 3.14.0 is the latest version of Python, which was released
on Oct. 2024.

Why the Name Python?


There is a fact behind choosing the name Python. Guido van Rossum was reading the script of a popular BBC
comedy series "Monty Python's Flying Circus". It was late on-air 1970s.

Van Rossum wanted to select a name which unique, sort, and little-bit mysterious. So he decided to select
naming Python after the "Monty Python's Flying Circus" for their newly created programming language.

The comedy series was creative and well random. It talks about everything. Thus it is slow and unpredictable,
which made it very interesting.

Python Features
Python provides many useful features which make it popular and valuable from the other programming
languages. It supports object-oriented programming, procedural programming approaches and provides
dynamic memory allocation. We have listed below a few essential features.

1) Easy to Learn and Use

Python is easy to learn as compared to other programming languages. Its syntax is straightforward and much
the same as the English language. There is no use of the semicolon or curly-bracket, the indentation defines the
code block. It is the recommended programming language for beginners.

2) Expressive Language

Python can perform complex tasks using a few lines of code. A simple example, the hello world program you
simply type print("Hello World"). It will take only one line to execute, while Java or C takes multiple lines.

3) Interpreted Language

Python is an interpreted language; it means the Python program is executed one line at a time. The advantage
of being interpreted language, it makes debugging easy and portable. Python is processed at runtime by the
interpreter. You do not need to compile your program before executing it. This is similar to and PHP.

4) Cross-platform Language
Python can run equally on different platforms such as Windows, Linux, UNIX, and Macintosh, etc. So, we can say
that Python is a portable language. It enables programmers to develop the software for several competing
platforms by writing a program only once.

5) Free and Open Source

Python is freely available for everyone. It is freely available on its official website [Link]. It has a large
community across the world that is dedicatedly working towards make new python modules and functions. The
open-source means, "Anyone can download its source code without paying any penny."

6) Object-Oriented Language

Python supports object-oriented language and concepts of classes and objects come into existence. It supports
inheritance, polymorphism, and encapsulation, etc. The object-oriented procedure helps to programmer to write
reusable code and develop applications in less code.

7) Large Standard Library

It provides a vast range of libraries for the various fields such as machine learning, web developer, and also for
the scripting. There are various machine learning libraries, such as Tensor flow, Pandas, Numpy, Keras, and
Pytorch, etc. Django, flask, pyramids are the popular framework for Python web development.

8) GUI Programming Support

Graphical User Interface is used for the developing Desktop application. PyQT5, Tkinter, Kivy are the libraries
which are used for developing the web application.

9. Embeddable

The code of the other programming language can use in the Python source code. We can use Python source
code in another programming language as well. It can embed other language into our code.

10. Dynamic Memory Allocation

In Python, we don't need to specify the data-type of the variable. When we assign some value to the variable, it
automatically allocates the memory to the variable at run time. Suppose we are assigned integer value 15
to x, then we don't need to write int x = 15. Just write x = 15.

Where is Python used?


Python is a general-purpose, popular programming language, and it is used in almost every technical field. The
various areas of Python use are given below.

o Data Science: Data Science is a vast field, and Python is an important language for this field because of
its simplicity, ease of use, and availability of powerful data analysis and visualization libraries like
NumPy, Pandas, and Matplotlib.
o Desktop Applications: PyQt and Tkinter are useful libraries that can be used in GUI - Graphical User
Interface-based Desktop Applications. There are better languages for this field, but it can be used with
other languages for making Applications.
o Console-based Applications: Python is also commonly used to create command-line or console-based
applications because of its ease of use and support for advanced features such as input/output
redirection and piping.
o Mobile Applications: While Python is not commonly used for creating mobile applications, it can still be
combined with frameworks like Kivy or BeeWare to create cross-platform mobile applications.
o Software Development: Python is considered one of the best software-making languages. Python is
easily compatible with both from Small Scale to Large Scale software.
o Artificial Intelligence: AI is an emerging Technology, and Python is a perfect language for artificial
intelligence and machine learning because of the availability of powerful libraries such as TensorFlow,
Keras, and PyTorch.
o Web Applications: Python is commonly used in web development on the backend with frameworks like
Django and Flask and on the front end with tools like JavaScript and HTML.
o Enterprise Applications: Python can be used to develop large-scale enterprise applications with
features such as distributed computing, networking, and parallel processing.
o Machine Learning: Python is widely used for machine learning due to its simplicity, ease of use, and
availability of powerful machine learning libraries.
o Speech Recognition: Python can be used for speech recognition applications through libraries such as
SpeechRecognition and PyAudio.
o Scientific computing: Libraries like NumPy, SciPy, and Pandas provide advanced numerical computing
capabilities for tasks like data analysis, machine learning, and more.
o Education: Python's easy-to-learn syntax and availability of many resources make it an ideal language
for teaching programming to beginners.
o Testing: Python is used for writing automated tests, providing frameworks like unit tests and pytest that
help write test cases and generate reports.
o Gaming: Python has libraries like Pygame, which provide a platform for developing games using Python.

Python Installation on Windows


Visit the link [Link] to download the latest release of Python. In this process, we will install
Python 3.11.3 on our Windows operating system. When we click on the above link, it will bring us the following
page.

Step - 1: Select the Python's version to download.

Click on the download button to download the exe file of Python.

If in case you want to download the specific version of Python. Then, you can scroll down further below to see
different versions from 2 and 3 respectively. Click on download button right next to the version number you
want to download.
Step - 2: Click on the Install Now

Double-click the executable file, which is downloaded.

The following window will open. Click on the Add Path check box, it will set the Python path automatically.

Now, Select Customize installation and proceed. We can also click on the customize installation to choose
desired location and features. Other important thing is install launcher for the all user must be checked.

Here, under the advanced options, click on the checkboxes of " Install Python 3.11 for all users ", which is
previously not checked in. This will checks the other option " Precompile standard library " automatically. And
the location of the installation will also be changed. We can change it later, so we leave the install location
default. Then, click on the install button to finally install.

Step - 3 Installation in Process


The set up is in progress. All the python libraries, packages, and other python default files will be installed in our
system. Once the installation is successful, the following page will appear saying " Setup was successful ".

Step - 4: Verifying the Python Installation

To verify whether the python is installed or not in our system, we have to do the following.

o Go to "Start" button, and search " cmd ".


o Then type, " python - - version ".
o If python is successfully installed, then we can see the version of the python installed.
o If not installed, then it will print the error as " 'python' is not recognized as an internal or external
command, operable program or batch file. ".

We are ready to work with the Python.

Step - 5: Opening idle

Now, to work on our first python program, we will go the interactive interpreter prompt(idle). To open this, go to
"Start" and type idle. Then, click on open to start working on idle.
First Python Program
In this Section, we will discuss the basic syntax of Python, we will run a simple program to print Hello World on
the console.

Python provides us the two ways to run a program:

o Using Interactive interpreter prompt


o Using a script file

Let's discuss each one of them in detail.

Interactive interpreter prompt


Python provides us the feature to execute the Python statement one by one at the interactive prompt. It is
preferable in the case where we are concerned about the output of each line of our Python program.

It will open the following prompt where we can execute the Python statement and check their impact on the
console.

After writing the print statement, press the Enter key.

Here, we get the message "Hello World !" printed on the console.

Using a script file (Script Mode Programming)


The interpreter prompt is best to run the single-line statements of the code. However, we cannot write the code
every-time on the terminal. It is not suitable to write multiple lines of code.

Using the script mode, we can write multiple lines code into a file which can be executed later. For this purpose,
we need to open an editor like notepad, create a file named and save it with .py extension, which stands
for "Python". Now, we will implement the above example using the script mode.

1. print ("hello world"); #here, we have used print() function to print the message on the console.

To run this file named as [Link], we need to run the following command on the terminal.
Step - 1: Open the Python interactive shell, and click "File" then choose "New", it will open a new blank script
in which we can write our code.

Step -2: Now, write the code and press "Ctrl+S" to save the file.

Step - 3: After saving the code, we can run it by clicking "Run" or "Run Module". It will display the output to the
shell.

The output will be shown as follows.

Step - 4: Apart from that, we can also run the file using the operating system terminal. But, we should be
aware of the path of the directory where we have saved our file.

o Open the command line prompt and navigate to the directory.


o We need to type the python keyword, followed by the file name and hit enter to run the Python file.

Python Statement:
Instructions that a python interpreter can execute are called statements. Two types-

Single line statement

a=1
print(a)

Multi-line Statements
Multi-line statements are written into the notepad like an editor and saved it with .py extension. In the following
example, we have defined the execution of the multiple code lines using the Python script.

Code:

name = "Andrew Venis"


branch = "Computer Science"
age = "25"
print("My name is: ", name, )
print("My age is: ", age)

Script File:
Pros and Cons of Script Mode

The script mode has few advantages and disadvantages as well. Let's understand the following advantages of
running code in script mode.

o We can run multiple lines of code.


o Debugging is easy in script mode.
o It is appropriate for beginners and also for experts.

Let's see the disadvantages of the script mode.

o We have to save the code every time if we make any change in the code.
o It can be tedious when we run a single or a few lines of code.

Get Started with PyCharm


In our first program, we have used gedit on our CentOS as an editor. On Windows, we have an alternative like
notepad or notepad++ to edit the code. However, these editors are not used as IDE for python since they are
unable to show the syntax related suggestions.

JetBrains provides the most popular and a widely used cross-platform IDE PyCharm to run the python
programs.

PyCharm installation
As we have already stated, PyCharm is a cross-platform IDE, and hence it can be installed on a variety of the
operating systems. In this section of the tutorial, we will cover the installation process of PyCharm on
Windows, MacOS, CentOS, and Ubuntu.

Windows

Installing PyCharm on Windows is very simple. To install PyCharm on Windows operating system, visit the
link [Link] to download the
executable installer. Double click the installer (.exe) file and install PyCharm by clicking next at each step.

To create a first program to Pycharm follows the following step.

Step - 1. Open Pycharm editor. Click on "Create New Project" option to create new project.

Step - 2. Select a location to save the project.

 We can save the newly created project at desired memory location or can keep file location as it is but
atleast change the project default name untitled to "FirstProject" or something
meaningful.
 Pycharm automatically found the installed Python interpreter.
 After change the name click on the "Create" Button.
Step - 3. Click on "File" menu and select "New". By clicking "New" option it will show various file formats.
Select the "Python File".

Step - 4. Now type the name of the Python file and click on "OK". We have written the "FirstProgram".

Step - 5. Now type the first program - print("Hello World") then click on the "Run" menu to run program.

Step - 6. The output will appear at the bottom of the screen.

Python print() Function


The print() function prints the specified message to the screen, or other standard output device.
The message can be a string, or any other object, the object will be converted into a string before written to the
screen

Syntax
print(object(s), sep=separator, end=end, file=file, flush=flush)

Parameter Values
Parameter Description

object(s) Any object, and as many as you like. Will be converted to string before printed

sep='separator' Optional. Specify how to separate the objects, if there is more than one. Default is ' '

end='end' Optional. Specify what to print at the end. Default is '\n' (line feed)

file Optional. An object with a write method. Default is [Link]

flush Optional. A Boolean, specifying if the output is flushed (True) or buffered (False). Default is False

Example

print(5,10,25) print(5,10,25,sep='-') print(5,10,25,sep='*',end='&') print(5,10,25,sep='*')


print("testing")

Comments in Python
 Comments can be used to explain Python code.
 Comments can be used to make the code more readable.
 Comments can be used to prevent execution when testing code.
In Python, we can apply comments using the # hash character. The Python interpreter entirely ignores the lines
followed by a hash character. A good programmer always uses the comments to make code under stable.

Types of Comment

Python provides the facility to write comments in two ways- single line comment and multi-line comment.

Single-Line Comment - Single-Line comment starts with the hash # character followed by text for further
explanation.

#This is a comment print("Hello, #print("Hello,World!")


print("Hello, World!") World!") #This is a print("Hello,World!")
comment

Multiline Comments
Python does not really have a syntax for multiline comments.
To add a multiline comment you could insert a # for each line:

#This is a comment """


#written in This is a comment
#more than just one line written in
print("Hello, World!") more than just one line
"""
print("Hello, World!")

Python Variables
Python variables are the reserved memory locations used to store values with in a Python Program. This means that when you
create a variable you reserve some space in the memory. Based on the data type of a variable, Python interpreter allocates
memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to Python
variables, you can store integers, decimals or characters in these variables.
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python
variables:

 A variable name must start with a letter or the underscore character


 A variable name cannot start with a number
 A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
 Variable names are case-sensitive (age, Age and AGE are three different variables)
 A variable name cannot be any of the Python keywords.
 Examples of valid identifiers: a123, _n, n_9, etc.
 Examples of invalid identifiers: 1a, n%4, n 9, etc.

Consider the following valid variables name.

name = "A"
Name = "B"
naMe = "C"
NAME = "D"
n_a_m_e = "E"
_name = "F"
name_ = "G"
_name_ = "H"
na56me = "I"

1. print(name,Name,naMe,NAME,n_a_m_e, NAME, n_a_m_e, _name, name_,_name, na56me)

Output:

A B C D E D E F G F I

If the name of variable contains multiple words, we should use these naming patterns −

 Camel case − First letter is a lowercase, but first letter of each subsequent word is in uppercase. For example:
kmPerHour, pricePerLitre
 Pascal case − First letter of each word is in uppercase. For example: KmPerHour, PricePerLitre
 Snake case − Use single underscore (_) character to separate words. For example: km_per_hour, price_per_litre

Declaring Variable and Assigning Values


Python variables do not need explicit declaration to reserve memory space or you can say to create a variable. A Python
variable is created automatically when you assign a value to it. The equal sign (=) is used to assign values to variables.

name = "Devansh"
age = 20
marks = 80.50

print(name)
print(age)
print(marks)

Output:

Devansh
20
80.5

Multiple Assignment
Multiple assignments, also known as assigning values to multiple variables in a single statement, is a feature of Python.

1. Assigning single value to multiple variables

Eg:

x=y=z=50 x=y=z=50
print(x) print(x,y,z)
print(y)
print(z)

Output:

50
50
50

2. Assigning multiple values to multiple variables:

Eg:

a,b,c=5,10,1 x,y,z=10,20,30 x,y,z=10,20.5,"Ram" x,y,z=10,20.5,'R x = "Python "


5 print(x,y,z) print(x,y,z) am' y = "is "
print (a) print(x,y,z) z = "awesome"
print (b)
In the print() function, print(x + y + z)
you output multiple You can also use
print (c) variables, separated by a the + operator to output
comma: multiple variables:

Output:

5
10
15
Case-Sensitive

Variable names are case-sensitive.

x=4
A = "Sally"
print(x)
print(a)

Python Variable Types


There are two types of variables in Python - Local variable and Global variable. Let's understand the following
variables.

Local Variable

The variables that are declared within the function and have scope within the function are known as local
variables. Let's examine the following illustration.

Example –

# Declaring a function def add():


def add(): a = 20
# Defining local variables. They has scope only within a b = 30
function c=a+b
a = 20 print("The sum is:", c)
b = 30 add()
print(c)
c=a+b
We tried to use local variable outside their
print("The sum is:", c)
scope; it threw the NameError.
# Calling a function
add()

Global Variables

Global variables can be utilized all through the program, and its extension is in the whole program. Global
variables can be used inside or outside the [Link] default, a variable declared outside of the function
serves as the global variable.
x = 101 # Global variable
def mainFunction(): def mainFunction():
x = "global"
global x
print(x) x = 'Welcome To python'
def func():
mainFunction() print("x inside:", x) mainFunction()
print(x) print(x)
func()
print("x outside:", x)

The global Keyword


If you need to modify a global variable within a function, you can use the global keyword to declare that
the variable is global:
z = "test" x = 101
def mainFunction():
def func(): global x
global z x = 'Welcome To python'
z = "modified" print(x)
print("z inside:", z)
mainFunction()

func() print(x)
print("z outside:", z)

Delete a variable
We can delete the variable using the del keyword. The syntax is given below.

Syntax -

1. del <variable_name>

In the following example, we create a variable x and assign value to it. We deleted variable x,
and print it, we get the error "variable x is not defined". The variable x will no longer use
in future.

Example -

# Assigning a value to x
x=6
print(x)
# deleting a variable.
del x
print(x)

You might also like