AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
MODULE – 1
BASICS OF PYTHON
What is Python, Python Interpreter, Python language basics: Language Semantics, Data Types,
Variables, Basic Functions, Operators, Flow Control Statements, Data Structures and Sequences: List,
Tuple, Set, Dictionaries.
Practical:
1. Programs using conditional and looping constructs
2. Programs using different data frames like list, tuple, set and dictionary.
INTRODUCTION TO PYTHON:
Python is a high-level programming language known for its simple and readable syntax. It was
created in 1991 by Guido van Rossum to make programming easy to learn and use.
It allows writing programs with fewer lines of code, improving readability.
It automatically detects variable types at runtime, eliminating the need for explicit
declarations.
It is used in web development, data analysis, automation, and many other fields.
It supports object-oriented, functional, and procedural programming styles.
PYTHON INTERPRETER
A Python Interpreter is the program that reads and executes Python code. It translates your Python
instructions into machine-readable form line by line, so the computer can understand and run them.
When you run a Python program, code passes through Python Interpreter, which is responsible for:
Checking your code for errors.
Converting it into an intermediate form called bytecode.
Sending it to the Python Virtual Machine (PVM) for execution.
1
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
Working of Interpreter
Let’s understand the step-by-step process of how your Python code runs:
1. Python Source Code (.py file): You write your program in a .py file.
2. Parser and AST (Syntax Check -> Abstract Syntax Tree): Python checks the code for syntax
errors and converts it into an AST (Abstract Syntax Tree), which represents the program structure.
3. Bytecode (.pyc file in __pycache__): The AST is compiled into bytecode, a low-level instruction
set. This bytecode may also be saved in the __pycache__ folder for reuse.
4. Python Virtual Machine (Executes Bytecode): The PVM executes the bytecode line by line and
translates it into machine instructions.
5. Output (Result on Screen): Finally, the program’s result is shown on the screen (for example, via
print()).
To visualize this at a high level, here’s a simple diagram of how an interpreter processes your code:
PYTHON LANGUAGE BASICS:
LANGUAGE SEMANTICS:
Different Python Interpreter Variants
2
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
The default and most widely used interpreter is called CPython, because it is implemented in the C
programming language.
CPython is the official Python interpreter you download from [Link]
Every time you type python [Link], you are using CPython unless you install another interpreter.
Other variants of Python interpreters include:
PyPy: faster, uses JIT (Just-In-Time compilation).
Stackless Python: optimized for concurrency.
MicroPython: lightweight, designed for microcontrollers.
Jython: runs on the Java platform.
IronPython: runs on .NET framework.
Difference between Compilers and Interpreters
It’s important to understand how an interpreter differs from a compiler:
Compiler Interpreter
It translates a program on a single run It executes a program line by line.
It is an Fast Process It is a Slow Process
It does not generate any form of
It generates output in the form of .(exe)
outputs.
It utilizes CPU more. It takes less utilization of CPU
It is maximum used in Programming
It is used in Production environment
and development environment.
Python, Ruby, PHP are the interpreted
C, C++, C# are the compiled languages
languages.
3
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
Compiler Interpreter
It takes lot of time to analyze the code It takes less time compared to
structure compilers.
Advantages
Easy debugging: errors are shown immediately line by line.
Flexible and portable: works across platforms without recompiling.
No need for separate compilation: just run the script directly.
Supports dynamic typing: variable types don’t need to be declared.
Disadvantages
Slower than compiled languages (because it executes line by line).
Higher memory usage.
Not ideal for low-level programming (like OS kernels or device drivers).
Python Variables
A memory location set aside to hold a value is called a variable. Python's variable type is
determined by the values provided to it, unlike other programming languages where variables need to have
their types explicitly stated. Python does not require an explicit declaration in order to reserve memory.
Assigning value to variables:
The type of variable is automatically determined by the interpreter based on the data it holds or is assigned.
The equal sign(=), also referred to as the assignment operator, is used to set a value for the variable.
4
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
Python Keywords
Each language has words and rules that make sense when put together in a sentence. Also, the
computer language Python has a set of predefined words that are called Keywords
Introduction to Python use these words anywhere else in Python because they have special
meanings. Keywords set the rules for how the code is written. That word can't be used as a variable,
function, or symbol name. The only words in Python that are written in capital letters are True and False.
Data Types
The variable sets aside a stored in memory to store a value, and when it is given a value, that value
is stored in that stored. Now, what kind of data that variable is linked to determines how much memory it
takes up. In other words, the data types tell you how much memory you need to store the value.
5
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
Python has 6 standard data types:
1. Numbers
2. String
3. List
4. Tuple
5. Dictionary
6. Set
Python Numbers
Based on their names, these are the types of data that store numbers: integer, float, and complex. It
can be either an int or a long int.
There are three numbers in Python:
Example:
A = 20 # Assing 20 to A
B = 4.67 # Assign 4.67 to B
6
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
print(A) # prints 20 on screen
print(B) # prints 4.65 on screen
Output: 20 4.65
Python Strings
Strings in Python are groups of characters that are kept in memory together, like an array of
characters. Either a single quote or two double quotes are used to show these characters.
Example:
S = ― Happy “ #prints Happy to console
print S[1] # prints first character to console
print S + ― Morning ― # concatenates Morning to Happy and prints on console
Output:
Happy
Happy Morning
Python Boolean Data Type:
The datatype which returns only 2 values either TURE or FALSE.
Example:
A = 50 ;
7
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
Output: >>> A = = 40 >>> FALSE
Python Operators
Operators are unique symbols or keywords in Python that perform operations on values and
variables. They form the foundation of expressions, which are used to work with data and carry out
calculations. Python has a number of operators, each having a distinct function.
The Python programming language supports the following types of operators:
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Assignment Operators
4. Logical Operators
5. Bitwise Operators
6. Membership Operators
Python Arithmetic Operators Common mathematical operations in addition modules are addition,
subtraction, multiplication, and division; additional arithmetic operations include exponential and floor
divisions are shown in Table Expressions, variables, and integers are supported by all.
8
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
9
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
Once the two variables "x" and "y" are defined, this code does a number of mathematical
operations, including floor division, modulus, addition, subtraction, multiplication, and division, and
reports the results.
Python Comparison Operators
10
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
Python comparison operators are required in order to compare two values. They produce a Boolean
value (True or False) based on the comparison.
11
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
Python Assignment Operators
To assign values to Python, utilize the assignment operators. The simplest assignment operator is
the single equal symbol (=). The variable on the operator's left side is given the value on the operator's right side.
12
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
Python Bitwise Operators
13
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
Bitwise operators in Python carry out actions on discrete binary integer [Link] operate on each
bit location logically while working with integer binary [Link] bitwise operations, including
AND (&), OR (|), NOT (), XOR (), left shift (), and right shift (>>), are included in Python.
Python Logical Operators
Boolean expressions are composed, and their truth values are evaluated using logical operators in
Python. They are necessary for controlling the program's execution flow and for creating conditional
statements. The three fundamental logical operators in Python are AND, OR, and NOT.
Python Membership Operators
To determine whether a particular value appears in a series or not, one can utilize Python
membership operators. They simplify the process of figuring out which elements belong in many types of
data structures, including sets, tuples, lists, and strings. The is and is not operators are the two main
membership operators in Python.
TYPES OF CONDITIONAL STATEMENT
As is the case with other programming languages, Python has four distinct types of conditional
statements, which are provided in the following order:
if Statements
if-Else Statements
elif Statements
Nested If-Else Statements
1. If Statements
The if statement in Python is one of the conditional statements that is used the most frequently in
programming languages. In this way, it determines whether or not particular statements are required to be
14
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
executed. It performs a check to determine whether a particular condition is satisfied; if the condition is
satisfied, the set of code included within the "if" block will be run; otherwise, it will not be executed.
Syntax:
if ( EXPRESSION = = TRUE ) :
if- Block of code
Next statement after Block of code is executed.
In the syntax presented above,
if the expression "EXPRESSION = = TRUE" is successfully executed, then the conditional block
of code will be run
Otherwise, the statement that comes after the conditional block of code will be executed.
15
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
2. if-else statements
The Boolean expression is evaluated by the if-else statement. The code in the "if" block will be
executed if the condition is TRUE; otherwise, the code in the "else" block will be executed.
Syntax:
If (EXPRESSION == TRUE):
If-Statement (Body of the block)
else:
else-Statement (Body of the block)
When the syntax (EXPRESSION = = TRUE) in the following example is successfully executed, a
block of code will be executed if it is not, otherwise it will be executed.
16
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
17
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
3. elif statements
"elif" statements are an additional type of conditional statement in Python. The "elif" statement
checks for multiple conditions only in the event that the supplied condition is false. The sole distinction
between it and a "if-else" expression is that the condition will be checked in "elif" rather than "else."
Syntax:
if (EXPRESSION-1 = = TRUE):
If-Statement (Body of the block)
elif(EXPRESSION-2 = = TRUE):
elif-Statement (Body of the block)
elif(EXPRESSION-3 = = TRUE):
elif-Statement (Body of the block)
else:
else-Statement (Body of the block)
In the above syntax ( EXPRESSION-1 = = TRUE ) is executed successfully then if- Block of
code will be executed
otherwise ( EXPRESSION-2 = = TRUE ) is tested, if it is executed successfully then elif- Block
of code related EXPRESSION-2 will be executed
18
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
otherwise ( EXPRESSION-3 = = TRUE ) is tested, if it is executed successfully then elif- Block
of code related EXPRESSION-3 will be executed otherwise else Block of code will be executed.
19
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
3. Nested if-else statements
Nested "if-else" statements indicate that one "if" or "if-else" statement is contained within another
if or if-else block. Python has this feature as well, which allows us to verify several conditions in a single
application.
Syntax:
if (EXPRESSION-1 = = TRUE):
if (EXPRESSION-2 = = TRUE):
20
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
Inner-If-Statement (Body of the block)
else:
else:
Inner-else-Statement (Body of the block)
Outer-else-Statement (Body of the block)
The syntax used above obviously shows that the if block will include another if block, and so on. If
block can have 'n' number of if blocks within it.
In the below flow chart, (EXPRESSION-1 = = TRUE) is executed successfully, then
(EXPRESSION-2 = = TRUE) is tested;
if it is executed successfully, then if- Block of code related EXPRESSION-2 will be executed;
otherwise, Block of code related EXPRESSION-2 will be executed; otherwise, Block of code
related EXPRESSION-1 will be executed.
21
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
TYPES OF LOOP STATEMENTS
As is the case with other programming languages, Python has three distinct types of looping statements,
which are provided in the following order:
For Loop Statement
While Loop Statements
Nested Loop Statement
For Loop Statement
It is possible to iterate over a series of elements in Python by using the for loop, which is one of the looping
instructions contained inside the language. There are a variety of objects that can be iterated, including a
list, a tuple, a text, and any other object.
Syntax:
for variable in sequence:
# Code block to be executed
The preceding syntax,
variable is a temporary variable that stores the value of each element in the sequence during each
iteration of the loop
The code block that comes after the for statement is carried out many times for each individual element
that is included in the sequence.
Example:
for i in 10:
# Code block to be executed Total 10 time block will be repeated
22
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
Data Structures and sequence:
List:
To determine how many items a list has, use the len() function:
Example
Print the number of items in the list:
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
Output:
List items can be of any data type:
Example
23
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1
AD25201- PYTHON FOR DATA SCIENCE R-25(2025-26) UNIT -I BASICS OF PYTHON
String, int and boolean data types:
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
Output:
What will be the result of the following syntax:
mylist = ['apple', 'banana', 'cherry']
print(mylist[1])
Output:
Banana
24
PREPARED BY,
ARCHANA VISHVESWARI R S
HOD/AI&DS, VVIT,DPI-1