Introduction to
Python
Dr. REVATHI T
[Link]@[Link]
Basic Programming Language-concepts
A programming language is a set of instructions and syntax used to create software
programs. Some of the key features of programming languages include:
• Algorithm: A step-by-step procedure for solving a problem or performing a task.
• Variable: A named storage location in memory that holds a value or data.
• Data Type: A classification that specifies what type of data a variable can hold,
such as integer, string, or boolean.
• Function: A self-contained block of code that performs a specific task and can be
called from other parts of the program.
• Control Flow: The order in which statements are executed in a program,
including loops and conditional statements.
• Syntax: The set of rules that govern the structure and format of a
programming language.
• Comment: A piece of text in a program that is ignored by the compiler or
interpreter, used to add notes or explanations to the code.
• Debugging: The process of finding and fixing errors or bugs in a program.
• IDE: Integrated Development Environment, a software application that
provides a comprehensive development environment for coding,
debugging, and testing.
• Operator: A symbol or keyword that represents an action or operation to
be performed on one or more values or variables, such as + (addition), –
(subtraction), * (multiplication), and / (division).
• Statement: A single line or instruction in a program that performs a
specific action or operation.
Python
• Python is a simple, general purpose, high level, and
object-oriented programming language.
• Python is an interpreted scripting language also. Guido
Van Rossum is known as the founder of Python
programming
Python 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.
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. Anyone
can contribute to the Python community
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) Extensible
It implies that other languages such as C/C++ can be used to compile the code and
thus it can be used further in our Python code. It converts the program into byte
code, and any platform can use that byte code.
8) 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.
9) 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.
10) Integrated
It can be easily integrated with languages like C, C++, and JAVA, etc. Python runs code line by line
like C,C++ Java. It makes easy to debug the code.
11. 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.
12. 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.
Python Applications
• Web Development
• Artificial Intelligence and Machine Learning
• Game Development
• Desktop GUI Development
• Software Development
• Scientific and Numeric Computations
• Web Scraping
• Integration to Embedded Systems
• Building CAD Apps
Various Python IDE
1. PyCharm
PyCharm comes in three versions: Apache-licensed Community version, Educational (Edu)
version, and proprietary Professional version. The first two versions are open source and free
of charge, but you’ll have to pay for the Professional version.
Advantages:
• Active community support
• Lots of useful plugins and productivity shortcuts
• Executes, edits, and debugs Python code without any external requirements
Disadvantages:
• Professional version is rather expensive
• Free tier offers less functionality than the paid versions
• Community version does not allow other programming languages to be used
IDLE
• IDLE, or Integrated Development and Learning Environment, is a free IDE that comes
bundled with any Python installation. It allows you to edit, run, and debug Python code
in a simple Python shell and code editor environment, making it great for beginners.
Advantages:
• User-friendly for beginners
• Offers features like syntax highlighting, search, auto-completion, & smart indent
• Integrated debugger with stepping & breakpoint features
Disadvantages:
• May be too simple for some
• Not suitable for large projects
• Lacks the advanced features offered by other IDEs
Visual Studio Code
Developed by Microsoft for Windows, Linux and macOS, VS Code is a free and
extensible code editor (not to be confused with Visual Studio). VS Code is a small
but comprehensive open-source code editor with an MIT license.
Advantages:
• Advanced customization with 30,000+ extensions in circulation
• Powerful code management engine
• You can import keyboard shortcuts from Python editors like Sublime Text or Atom
Disadvantages:
• Initial customization can be overwhelming/difficult due to the number of available
extensions
• Extensions can be unreliable at times
Jupyter Notebook
• Jupyter Notebook is a free and open-source IDE that is
used to create Jupyter documents. Uniquely, it is a web-
based IDE that allows you to easily share your work.
• Jupyter Notebook is very popular among data scientists
as it’s great for machine learning, simulations, data
visualizations, data analysis, and also for sharing and
presenting findings. Jupyter is ready to work right out of
the box, requiring little to no configuration. This makes
it a perfect tool for quick Python scripting.
Advantages:
• Easy to use and learn
• Supports 40+ different languages
• Easy to share work
Disadvantages:
• Mostly suited to development in the fields of data and scientific
computing
• Lacks advanced features offered by other IDEs
• Coding within independent cells may be difficult to manage as the cell
count increases
Spyder
• Spyder offers a unique combination of advanced analysis, debugging, editing, interactive
execution, in-depth inspection, and visualization capabilities that are comparable to
scientific software packages. Its main components are the interactive console, the
documentation viewer, the variable explorer, and development tools.
Advantages:
• Community support
• Rich set of development tools
• Comprehensive documentation
Disadvantages:
• Execution dependencies
• Optional dependencies
• Steep learning curve
Anaconda
Anaconda is the Open Source data science distribution for the Python
and R programming languages scientific computing, such as data
science, machine learning applications, predictive analysis, large-scale
data processing, etc. The Anaconda can be used to simplify package
management and deployment. It includes more than 300 data science
packages that are suitable for Windows, Linux, and MacOS
Downloading and Installing
Anaconda
• Step 1: Go to [Link] and click
Download
• Step 2: Click on the next button to continue with the
installation and click on the I agree with the next page's
button
Step 3: Now, select the option according to your
requirement. If you want to choose to get it used by a
single user, it recommends selecting "Just me.".
Step 4: Now, choose the location where you want to
install Anaconda.
Step 5:Register Anaconda as your default Python to allow
other programs.
Step 6: Now, wait till the progress bar completes and press the button
Next.
Step 7: Finally, Click on the Finish button to complete the
installation
Access Jupyter Notebook
• In windows search type: Anaconda Navigator
• Go to jupyter notebook
• Click launch button
• To create new jupyter notebook:
• Click new ->Python 3
• To rename the file :
• Click untitled and rename
Data Types
• A variable can contain a variety of values. The storage method for each of
the standard data types that Python provides is specified by Python. The
following is a list of the Python-defined data types.
[Link]
[Link] Type
[Link]
[Link]
[Link]
Numbers
• Numeric values are stored in numbers. The whole number, float, and
complex qualities have a place with a Python Numbers datatype.
Python offers the type() function to determine a variable's data type
• Int: Whole number worth can be any length, like numbers 10, 2, 29, -
20, - 150, and so on. An integer can be any length you want in Python.
Its worth has a place with int.
• Float: Float stores drifting point numbers like 1.9, 9.902, 15.2, etc. It
can be accurate to within 15 decimal places.
• Complex: An intricate number contains an arranged pair, i.e., x + iy,
where x and y signify the genuine and non-existent parts separately.
The complex numbers like 2.14j, 2.0 + 2.3j, etc.
• Sequence Type
String
The sequence of characters in the quotation marks can be used to
describe the string.
List: Python Lists are just like dynamically sized array
Tuple: Tuples are used to store multiple items in a single variable.
Boolean
In programming you often need to know if an expression is True or False.
You can evaluate any expression in Python, and get one of two
answers, True or False.
SET:
Sets are used to store multiple items in a single variable.
Dictionary
Dictionaries are used to store data values in key:value pairs.
• A dictionary is a collection which is ordered*, changeable and do not allow
duplicates.
Python divides the operators in the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Arithmetic Operators
Operator Operation Example
+ Addition 5+2=7
- Subtraction 4-2=2
* Multiplication 2*3=6
/ Division 4/2=2
// Floor Division 10 // 3 = 3
% Modulo 5%2=1
** Power 4 ** 2 = 16
Python Assignment Operators
Assignment operators are used to assign values to variables.
Operator Name Example
Assignment
= Operator a=7
Addition
+= Assignment a += 1 # a = a + 1
Subtraction
-= Assignment a -= 3 # a = a - 3
Multiplication
*= Assignment a *= 4 # a = a * 4
/= Division Assignment a /= 3 # a = a / 3
Remainder
%= Assignment a %= 10 # a = a % 10
Exponent a **= 10 # a = a **
**= Assignment 10
Python Comparison Operators
Comparison operators compare two values/variables and return a
boolean result:
Operator Meaning Example
== Is Equal To 3 == 5 gives us False
!= Not Equal To 3 != 5 gives us True
> Greater Than 3 > 5 gives us False
< Less Than 3 < 5 gives us True
Greater Than or
>= 3 >= 5 give us False
Equal To
Less Than or Equal
<= 3 <= 5 gives us True
To
Python Logical Operators
Logical operators are used to check whether an expression
is True or False. They are used in decision-making.
Operator Example Meaning
Logical AND:
and a and b True only if both the
operands are True
Logical OR:
True if at least one
or a or b
of the operands is
True
Logical NOT:
True if the operand
not not a
is False and vice-
versa.
Python Bitwise operators
• Bitwise operators act on operands as if they were
strings of binary digits. They operate bit by bit.
Operator Meaning Example
& Bitwise AND x & y = 0 (0000 0000)
| Bitwise OR x | y = 14 (0000
1110)
~ Bitwise NOT ~x = -11 (1111 0101)
^ Bitwise XOR x ^ y = 14 (0000
1110)
>> Bitwise right shift x >> 2 = 2 (0000
0010)
<< Bitwise left shift x << 2 = 40 (0010
1000)
Python Special Operators
• Python language offers some special types of operators
like the identity operator and
the membership
Identity operators operator.
In Python, is and is not are used to check if two values are
located on the same part of the memory. Two variables that
are equal does not Meaning
Operator imply that they are identical.
Example
True if the operands
is are identical (refer to x is True
the same object)
True if the operands
are not identical (do
is not x is not True
not refer to the
same object)
Membership operators
In Python, in and not in are the membership operators. They
are used to test whether a value or variable is found in a
sequence (string, list, tuple, set and dictionary).
In a dictionary we can only test for presence of key, not the
value.
Operator Meaning Example
True if
value/variable is
in 5 in x
found in the
sequence
True if
value/variable is not
not in 5 not in x
found in the
sequence
Math Functions
Python has a set of built-in math functions, including an
extensive math module, that allows you to perform
mathematical tasks on numbers
1)min()
2)max()
3)abs()
4pow()
The Math Module
Python has also a built-in module called math, which extends
the list of mathematical functions.
To use it, you must import the math module:
import math
1)[Link]()
2)[Link]()
3)[Link]()
4)[Link]()