MODULE -1
INTRODUCTION TO PYTHON
PYTHON FOR DATA SCIENCE,DEPT OF AI&ML
Introduction to python
Introduction to Python
Python is a powerful, high-level programming language known for its simplicity and readability. Created by
Guido van Rossum and first released in 1991, Python is widely used for various applications, including web
development, data analysis, artificial intelligence, and scientific computing.
Key features of Python include
Easy syntax: Python’s syntax is clear and easy to learn,making it a great choice for beginners.
Interpreted language: Python code is executed line by line, which makes it easier to test and debug.
Versatile and dynamic: It supports multiple programming paradigms like procedural, object-
oriented, and functional programming.
Extensive libraries: Python has a rich collection of libraries and frameworks (like NumPy, Django, and
Pandas) that simplify coding for various applications.
Elements of Python Language
Python has a defined set of valid characters recognized by the language. These are categorized into two main
character sets
1. Source Characters
Alphabets: Uppercase (A-Z) and lowercase (a-z), including the underscore (_).
Special Characters: This includes symbols like +, -, *, /, %, &,
|, (,), [, ], ?, and more.
2.Tokens: Tokens are the smallest lexical units in Python programs and include
1. Identifiers: Names given to variables, functions, etc. They must start with a letter or underscore,
cannot contain spaces, and are case-sensitive.
2. Keywords: Reserved words with specific meanings like and, for, def, class, return, etc.
3. Literals: Fixed values, such as numbers (integer, float, complex), strings, and booleans
(True, False).
4. Punctuation: Special characters used in the syntax of the language, such as brackets and operators.
3.Identifier: Identifiers are names used to represent entities in Python. They must start with a letter or
underscore. They cannot contain spaces and are case-sensitive.
4.Keywords: Keywords are reserved for specific functions in Python. There are total 35 keywords in python.
Literals: Literals are constant values that don't change during execution
1. String Literals: Represented with single, double, or triple quotes.
Example: Single-quoted strings: 'hello', 'goodbye'
Double-quoted strings: "hello", "goodbye"
Triple-quoted strings: '''hello\nworld''', """hello\nworld"".
Python for data science, dept of AI&ML
Introduction to python
2. Numerical Literals: Integers, floats, and complex numbers.
Integers: 10, -20, 0
Floating Point Literals: 3.14, -0.5, 1.0e10 Complex Literals: 3+4j, -2-3j
Octal Literals: 0o12, 0o77
Hexadecimal Literals: 0x1a, 0xdeadbeef
3. Boolean Literals: Represented by True and False.
True
False
Punctuation in Python
Punctuation characters are used to structure and organize Python code. They play a vital role in defining
blocks, operations, and statements within the language. Some key punctuation symbols include:
Comma ( , ): Used to separate elements, such as in function arguments or lists.
Colon ( : ): Used in function definitions, loops, and conditionals to indicate the start of an indented block.
Period ( . ): Used to access methods or properties of objects.
Semicolon ( ; ): Rarely used but can separate multiple statements on the same line.
Parentheses ( ): Used for function calls and to group expressions.
Square brackets [ ] : Used to access elements in lists, arrays, or define lists.
Curly braces { } : Used to define dictionaries or sets. Quotation marks ( ' ' or " " ): Used for string
literals.
Backslash ( \ ) : Used for escape sequences in strings.
Python block structure:python uses indentation mechanism to group the statements. All statements
with the same distance from left side are considered as block or group. In Python, indentation is the use of
spaces or tabs at the beginning of a line of code to indicate a block of code or the level of
hierarchy in a program.
Fig : Python block structure
Python for data science, dept of AI&ML
Introduction to python
Example
In Python, comments are a critical feature that allows programmers to embed explanations
within their code. These comments are essential for improving code readability and
maintainability, helping both the original developer and others who may work with the
code in the future to understand its logic and purpose. Python supports two types of
comments: single-line comments and multi-line comments.
Single-line comments in Python begin with the hash symbol (#) and extend to the end of the
line. These comments are used to annotate a specific line of code or clarify its intent. Since
Python ignores everything after the hash symbol, single-line comments can be added
anywhere in the code, even after an executable statement.
For example:x = 5 # This is a single-line comment explaining the assignment
Multi-line comments, though not officially recognized as a separate feature in Python, can
be simulated using multiple single-line comments or docstrings (although the latter is
primarily intended for documentation purposes rather than commenting). Multiple single-
line comments are simply stacked
# This is a multi-line comment
# Each line starts with a hash symbol
Alternatively, docstrings are enclosed within triple quotes (''' or """). While used to
document functions, classes, or modules, they can serve as multi-line comments in code
blocks, though this is not their primary purpose:
"""
This is a multi-line comment using a docstring It can span multiple
lines
"""
Python comments do not affect code execution, making them ideal for debugging, providing
clarification, or temporarily disabling parts of the code. Well-placed comments are a
hallmark of good programming practices. They help developers understand complex sections
of the code,reduce the learning curve for newcomers, and ensure that the code remains
maintainable in the long run.To sum up, comments in Python are invaluable tools for writing
clear, understandable, and sustainable code. They serve as annotations that facilitate easier
collaboration and debugging, ultimately improving the quality of the software.
Python for data science, dept of AI&ML
Introduction to python
Variables and Assignment Statements: Concepts related to variables and assignment
statements in Python:
VARIABLES: One can visualize a variable as a container. As the name indicates, its value can change
during the program execution. In Python, everything is an object. Therefore, a variable also is an object.
Thus, the rules for forming a variable are the same as the identifiers. The rules are stated below:
1) A variable can be of any length.
2) Variable names are case sensitive as abc , ABC, and abC are all different.
3) It should start with a character and follow by a character, digit, or underscore symbol (_) 4) Spaces,
dashes, punctuation marks, and quotations are not allowed.
5) Keywords are not be allowed.
No. Variable names Valid/invalid Remarks
1 y Valid All rules are followed
2 X1 Valid All rules are followed
3 Id_number Valid Underscore sign is permitted
4 While Valid While is differ from a while ,which is a
keyword
5 “g” Invalid The quote is not permitted
6 162yy Invalid A variable cannot start with a number
7 works Invalid A variable cannot have a space
8 while Invalid Keyword
9 Id! Invalid The special symbol ! Is not allowed
ASSIGNMENT STATEMENT:One of the most critical statement in python is the assignment statement
.The syntax of the introductory assignment is given follows:
SYNTAX <variable>=<expression>
Syntax of single variable
An expression can be a literal, a calculation, a functional call, or a combination of all. For example, the
following statement assigns a value of 3 to the
identifier.
>>>x=3
Let us analyse the statement x= 3 further. Here, x is called L-value or I-value. L- values are the objects to
which a value or expression is assigned. L-value can appear on the LHS or in RHS also as in statement x =
x + 5. However, the value five cannot appear on the LHS as 5 is R-value or r-value .R- values are literals or
expressions. The assignment statement assigns RHS to LHS and expressions.
An alternative form of an assignment statement is to assign/compute several values simultaneously. The
syntax of simultaneous assignments is given as follows:
Python for data science, dept of AI&ML
Introduction to python
SYNTAX <var 1>,<var 2>,….<var n> = <expr 1>,<expr 2>,…..<expr n>
Syntax of multiple variable
A simultaneous (or multiple) assignments is a powerful statement. It allows the initialization of non-variables
as shown below and swapping is done using the simultaneous assignment.
EXAMPLE
>>>x, y = 10,20
>>>x, y = y, x Output: (20,10)
>>>x, y
Data Types in Python: Exploring various data types available in Python.
A data type indicates what values a variable can hold, and an operator indicates the kind of operations
performed on the values. Attributes are information about objects. One crucial attribute is called its data
type. The data type is a kind of value that is assigned to variables. For example, x = 3 makes x an integer
variable as 3 is a whole number and an integer (as it is not a fraction number). Python supports many types
of data. They can be classified as basic data types and collections. One can check the data type using the
command type( ). It is often useful to check the data type of a value or variable. This command is handy
when the exact type of data is unknown.
The classification of data types is shown below One can check the data type using the command type(
). It is often useful to check the data type of a value or variable. This command is handy when the exact type
of data is unknown.
)Numbers
a) Integers
Int or integers are primary data types. Integers are whole numbers. It can be a positive or negative number
without a decimal point. The types of integers that Python can recognize are
1. Decimal integers: These are digits whose base is 10. Examples of decimal numbers are 45,
-96, 128, and 786.
2. Octal Integers: These are digits whose base is 8. These numbers are indicated by 00 or 0o (zero
followed by small/big 0)
Examples of octal numbers are 0028 and 0077.
3. Hexadecimal numbers: Hexadecimal numbers are numbers whose base is 16. These numbers use the
prefix OX or 0x (zero followed by small/big x)
NOTE - Integer numbers can be of any length limited only by the physical memory and by the python
language. Python can support long numbers. Real numbers are stored with double precision depending on the
hardware. Scientific notation is used for very large and very small numbers
Python for data science, dept of AI&ML
Introduction to python
b) Float number
Fractional numbers have a decimal point are called float numbers. Float numbers can be positive or
negative numbers. Examples of floating numbers are
14.36, 3.14
Float numbers also can be of unlimited range depending on the machine.
c) Complex numbers
Complex numbers are extensions of floating-point numbers with real and imaginary parts.
Complex numbers are used heavily in the scientific domain. The real and imaginary parts of the
complex number can be retrieved as shown in the below image:
1) Boolean types
A Boolean value has a data type called bool. A Boolean type refers to True and False. A Boolean
object can have only two values - True and False. It can be represented by integers as 0 and 1 as well.
Therefore, one can create a Boolean variable that can hold Boolean values.
The python statement creates a variable called time_up and sets its value to True. True and False are
built-in Python values. There is a Python function called bool that can convert values to Boolean also.
The zero or empty string value is False, and the rest of the values are True.
A Boolean expression returns a value that is true or false as one knows that an expression involves
operators and operands.
2) None Type
Python for data science, dept of AI&ML
Introduction to python
None is a special data type that indicates that there is no value. In all other programming languages, it
is called Null. One can check its type as
The default return value of a function is None.
Collection Data Types
Containers are a way to group objects of a similar kind. This is also known as collections. The objects
may be ordered or sometimes unordered. Two main kinds of collection types are sequence and
another type called mapping.
A sequence is an ordered collection of values. Three sequence types are string, list, and tuples.
a. A string is a sequence of characters
b. A list is a sequence of values
c. A tuple is a sequence of values like lists but a tuple is immutable
All elements in a sequence are stored as the contiguous series of memory locations that are indexed
from 0 onwards. The last element in the index is n-1. It can be numbered backward as -1 to - n also.
Common Operations on the sequence are –
i. Membership operations checks whether an element is present or not
ii. Operations like concatenation, where two sequences can be combined as seq 1+ seq 2
iii. Slicing operations like [start index: end index]
iv. Built-in operations like shown in the below table
3) Strings
The string is an example of a sequence collection data type. It is a collection of characters delimited
by single, double, and triple quotes. It can include letters, numbers, punctuations, and many special/
unprintable characters. Python allows us to specify strings using
a. Single quotes (‘ ’)
b. Double quotes (“ “)
c. Triple quotes. (“”” “””)
Some examples of strings are "lion" and '14.0. It is mostly preferred to use single quotes to reduce
typing effort, and triple quotes are used for comments. Strings are immutable as one can change
individual elements and the size of the list. Either can access the characters by the positive index or
negative index.
Python for data science, dept of AI&ML
Introduction to python
Operations in Python: Overview of different operations supported by Python
Strings support many operations. Some of the elementary operations using arithmetic operators are
shown in the figure below:
5) Lists
A list is a set of items or elements. All these items are separated by commas and enclosed within square
brackets. In all aspects, lists are similar to arrays but can have non-homogenous content.
To create a list, one can just enclose the sequence of objects in square brackets. For example, a list can
be a collection of integers, strings, and floating values.
Some of the list operations are:
Python for data science, dept of AI&ML
Introduction to python
Here, lstl is a list that consists of elements within "Hello", 'world', 13,3.14. A list is a sequence type as
the lists are indexed, and their contents are accessed. For example, 1st1[2] fetches 3.14. The index of
the first element is 0. The index of the last element is n-1.
Lists are mutable as the items can be changed. Some of the list operations are adding an element,
combing lists, insertion, deletion of items, and deletion of lists.
4) Tuples
A tuple is a collection of items or elements. A tuple is also a sort of list but the main difference is that
tuples are immutable compared to mutable lists. In other words, one cannot change the elements or
size in a tuple. The tuple items are enclosed in round brackets and separated by a comma. The empty
tuple is . The tuple with a single element is called a singleton. There is no restriction on the tuple
content and it can be another tuple also.
To generate tuples, just enclose the sequence of objects in ( ) and tuple support indexing/slicing, and
one cannot change the elements. Some of the examples are,
Tuple supports operations like indexing, slicing, combining tuples, deletion of tuples, insertion, and
deletion of elements in the tuple.
5) Bytes and Byte arrays
Bytes is an immutable object that stores a long sequence of values from 0 to 255. These are used to
print ASCIl and Unicode strings. One can use this as an index to access the bytes. This data type is
useful for encoding ASCII, Unicode 0 UTF-8, UTF - 16, images such as JPEG, PNG, and audio files
like MP3. The following segment is an example of how bytes can be defined.
# Store in a byte as indicated by the prefix b
The byte array is like a byte type useful for encoding images, Unicode, and ASCII strings. These are
similar to byes, except that byte arrays are immutable.
6) Dictionary
Python for data science, dept of AI&ML
Introduction to python
A dictionary is an unordered collection of items in the form of keys and values A dictionary is called
as a mapping type. A map type consists of a set of element pairs. The first element of the pair is called
a key, and the second element is called a value. To find the corresponding equivalent values, one can
search for a key or value.
A dictionary is created by a sequence of key-value pairs with curly brackets. It uses curly braces &
and a colon to separate key-value pairs. The key should be an immutable object. Dictionary facilitates
the addressing and changing of items using assignments. Some examples of dictionaries are given
below:
where 'abc', and 'def' are keys and '123' and '456' are associated passwords. The colon (;) is used to
separate the key and value. {} are used to enclose the whole dictionary structure.
Some of the important characteristics of the dictionary are:
a. The dictionary keys should be unique.
b) No two elements can have the same value.
c) Keys also should be immutable.
d) Values do not have such restrictions.
The dictionary operations are the creation of a dictionary, adding elements to the dictionary, removing
the elements of the dictionary, and deletion of a dictionary.
7) Sets/ Frozen Sets
A set is an unordered collection of zero or more elements with no duplicates. As in mathematics, a set
is an unordered collection of items with no duplicate items that use curly braces for sets- {}. The set
is immutable and unique. The following is a set of operations is as shown in Table Below.
Python for data science, dept of AI&ML
Introduction to python
It can be observed that the duplicate is removed. In addition, there are two types of sets— mutable sets
where one can add or remove elements, and frozen sets where after its creation process, the items
cannot be changed. Some of the set operations are the creation of a set, adding an element into the set,
removing an element into the set, the union of two sets, the intersection of sets, and set difference.
Frozenset is similar to sets except that frozensets are immutable.
OPERATORS IN PYTHON
TYPES OF OPERATORS
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
Python Arithmetic Operators
Assignment Operators
# assign 10 to a
a = 10
# assign 5 to b
b=5
# assign the sum of a and b to a
a += b #a=a+b
print(a)
Python for data science, dept of AI&ML
Introduction to python
# Output: 15
Python Comparison Operators
Example 3: Comparison Operators
a=5
b=2
# equal to operator
print('a == b =', a == b)
# not equal to operator
print('a != b =', a != b)
# greater than operator
print('a > b =', a > b)
# less than operator
print('a < b =', a < b)
# greater than or equal to operator
print('a >= b =', a >= b)
# less than or equal to operator
print('a <= b =', a <= b)
Python for data science, dept of AI&ML
Introduction to python
Python Logical Operators
Example : Logical Operators
# logical AND
print(True and True) # True
print(True and False) # False
# logical OR
print(True or False) # True
# logical NOT
print(not True) # False
Python Bitwise operators
Python for data science, dept of AI&ML
Introduction to python
Identity operators
Example : Identity operators in Python
x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
print(x1 is not y1) # prints False
print(x2 is y2) # prints True
print(x3 is y3) # prints False
Membership operators
Python for data science, dept of AI&ML
Introduction to python
Example 5: Membership operators in Python
message = 'Hello world'
dict1 = {1:'a', 2:'b'}
# check if 'H' is present in message string
print('H' in message) # prints True
# check if 'hello' is present in message string
print('hello' not in message) # prints True
# check if '1' key is present in dict1
print(1 in dict1) # prints True
# check if 'a' key is present in dict1
print('a' in dict1) # prints False
Functions for conversion to container type:
1.tuple(x):Converts the input x into a tuple.(A tuple is an immutable sequence, meaning once
created, its elements cannot be changed.)
Example:
lst = [1, 2, 3]
tpl = tuple(lst) # Converts list to
tuple print(tpl)
# Output: (1, 2, 3)
Python for data science, dept of AI&ML
Introduction to python
list(x):Converts the input x into a list.(A list is a mutable sequence, meaning you can add,
remove, or modify its elements.)
Example:
tpl = (1, 2, 3)
lst = list(tpl) # Converts tuple to
list print(lst)
# Output: [1, 2, 3]
set(x):Converts the input x into a set (a collection of unique elements.(A set is an unordered
collection of unique elements. It automatically removes duplicate values.)
Example:
lst = [1, 2, 2, 3]
s = set(lst) # Converts list to set, duplicates are removed
print(s)
# Output: {1, 2, 3}
bool():The bool() function converts a value to a boolean.(Booleans: Non-zero numbers and non-
empty collections are True, while zero, None, and empty collections are False.)
Example:
print(bool(0)) #Output: False
print(bool(1)) # Output: True
print(bool(97)) # Output: True
These conversions are commonly used in Python to ensure that the data type of a variable
matches the needs
Input/Output in Python:
Syntax: var=input()
Example 1:
str=input(“enter string”)
Enter string
Hello.
Example 2:
a=input(“Enter number”)
Enter number
10
Type error.
Python for data science, dept of AI&ML
Introduction to python
Eval
a=int(input(“enter number”)
Enter number
10
Print(a)
x=eval(input(“enter a number”))
Enter a number
10
Print(x)
Print statement
x=20
print(x)
Output:20
print(‘This is hello world’)
Output: This is hello world
Print(“Joyce's car”)
Output: Joyce's car
Print(“I am”, ”from India”, sep=“,”)
Print(“I am”, ”from India”, sep=“:”)
Print(“I am”, ”from India”, sep=“+”)
a=10
b=“This is string”
Print(a,b)
Python for data science, dept of AI&ML
Introduction to python
String Interpolation and string formatting
Python for data science, dept of AI&ML