PYTHON
PYTHON Introduction
Python is a high-level programming language.
It is an object-oriented programming language.
It is open source.
It is platform independent.
Why Python is Object-Oriented?
Python supports classes and objects which help in code reusability, security, maintenance, and
real-world problem solving.
Features of Python
Simple and easy-to-understand syntax
Open-source and free to use
Platform-independent
Supports Object-Oriented Programming (OOP)
Large standard library
Dynamically typed language
Applications of Python
Web Development
Data Analysis
Machine Learning and Artificial Intelligence
Automation and Scripting
Desktop Applications
Data Types
A data type specifies the type of value that a variable can store.
1. Numeric Data Types – int, float, complex
2. String Data Type – str
3. Sequence Data Types – list, tuple, range
4. Set Data Types – set, frozenset
5. Dictionary Data Type – dict
int_data = 2
print(int_data, type(int_data))
float_data = 5.0
print(float_data, type(float_data))
complex_data = 2 + 5j
print(complex_data, type(complex_data))
string_data = "good morning"
print(string_data, type(string_data))
list_data = [1, 2, 3, 4, 5, "good day"]
print(list_data, type(list_data))
tuple_data = (1, 2, 3, 4, "morning")
print(tuple_data, type(tuple_data))
set_data = {1, 2, 3, 4, 5, "monkey"}
print(set_data, type(set_data))
range_data = range(10)
print(range_data, type(range_data))
frozen_data = frozenset((1, 2, 3))
print(frozen_data, type(frozen_data))
dictionary_data = {
"id": "1",
"name": "Manasa"
}
print(dictionary_data, type(dictionary_data))
D/F B/W List tuple set string range dictionary
Feature List Tuple Set String Range Dictionary
Syntax [] () {} "" or '' range() {key:value}
Mutabilit Mutable Immutable Mutable Immutabl Immutabl Mutable
y e e
Order Ordered Ordered Unordered Ordered Ordered Ordered (Python
3.7+)
Duplicate Allowed Allowed Not Allowed Allowed Allowed Keys are unique
s
Data Allows Allows Allows Sequence Sequence Allows
Type heterogeneou heterogeneou heterogeneou of of heterogeneous
s data types s data types s data types characters integers data types for both
(immutable keys and values
elements
only)
Indexing Supported Supported Not Supporte Supporte Accessed using
Supported d d keys
Example [1, 2, (1, 2, {1, 2, 3} "Python" range(5) {"id":1,
"Python"] "Python") "name":"Manasa"
}
OPERATORS
Operators in Python are special symbols or keywords used to perform operations on variables, values,
and objects.
1. Arithmetic Operators: +, -, *, /, %, //, **
num_1 = 10
num_2 = 2
print(num_1 + num_2) # Addition
print(num_1 - num_2) # Subtraction
print(num_1 * num_2) # Multiplication
print(num_1 / num_2) # Division
print(num_1 ** num_2) # Exponentiation (Power)
print(num_1 // 2) # Floor Division
Output
12
8
20
5.0
100
5
2. Assignment Operators: =, +=, -=, *=, /=
num_1 = 10
num_2 = 2
num_1 += 5
print(num_1)
num_1 -= 6
print(num_1)
num_2 *= 2
print(num_2)
num_1 /= 3
print(num_1)
Output
15
9
4
3.0
[Link] Operators: >, <, >=, <=, ==, !=
num_1 = 10
num_2 = 2
print(num_1 > num_2)
print(num_1 < num_2)
print(num_1 >= num_2)
print(num_1 <= num_2)
print(num_1 == num_2)
print(num_1 != num_2)
Output
True
False
True
False
False
True
4. Logical Operators: and, or, not
num_1 = 10
num_2 = 2
# and operator
print(num_1 > num_2 and num_1 < 20)
# or operator
print(num_1 > num_2 or num_1 > 20)
# not operator
print(not(num_1 > num_2))
Output
True
True
False
[Link] Operators: in, not in
example = "python is fast growing technology"
print("Fast" in example)
print("growing" in example)
print("programming" not in example)
a_list = [1, 2.3, 2+3j, "good morning"]
print(1 in a_list)
print(2.3 not in a_list)
t_list = (3, 4.5, 6+7j, "hello")
print(3 in t_list)
print("hello" in t_list)
Output
False
True
True
True
False
True
True
[Link] Operators: is, is not
a = [1, 2, 3]
b = [1, 2, 3]
c=a
print(a == b)
print(a is b)
print(c is a)
Output
True
False
True
List and tuple data type method in python
List : A List is a collection of items stored in square brackets [].
1. append()
Definition: Adds a single element at the end of the list.
Example:
list_elements = [1, 26, "manasa", 4.5]
list_elements.append(3.5)
print(list_elements)
print(list_elements)
Output:
[1, 26, 'manasa', 4.5, 3.5]
2. insert()
Definition: Adds a single element at a specified index.
Example:
list_elements = [1, 26, "manasa", 4.5]
list_elements.insert(2, "devops")
print(list_elements)
Output:
[1, 26, 'devops', 'manasa', 4.5]
3. extend()
Definition: Adds multiple elements at the end of the list.
Example:
list_elements = [1, 2, 3]
list_elements.extend([4, 5, 6])
print(list_elements)
Output:
[1, 2, 3, 4, 5, 6]
4. pop()
Definition: Removes the last element from the list.
Example:
list_elements = [1, 2, 3, 4]
list_elements.pop()
print(list_elements)
Output
[1, 2, 3]
5. remove()
Definition: Removes an element by value.
Example:
list_elements = [1, 2, 3, 4]
list_elements.remove(2)
print(list_elements)
Output:
[1, 3, 4]
6. clear()
Definition: Removes all elements from the list.
Example:
list_elements = [1, 2, 3]
list_elements.clear()
print(list_elements)
Output:
[]
7. reverse()
Definition: Reverses the list.
Example:
list_elements = [1, 2, 3, 4]
list_elements.reverse()
print(list_elements)
Output:
[4, 3, 2, 1]
8. count()
Definition: Counts how many times an element occurs in a list.
Example:
list_elements = [1, 2, 2, 3, 2]
print(list_elements.count(2))
Output:
9. index()
Definition: Returns the index of the specified element.
Example:
list_elements = ["java", "python", "devops"]
print(list_elements.index("python"))
Output:
1
10. sort()
Definition: Sorts the list in ascending or descending order.
Example:
unsorted_elements = [8, 3, -2, -4]
unsorted_elements.sort()
print(unsorted_elements)
Output:
[-4, -2, 3, 8]
11. copy()
Definition: Creates a copy of the list.
Example:
list_elements = [1, 2, 3]
copied_elements = list_elements.copy()
print(copied_elements)
Output:
[1, 2, 3]
Tuple Methods
A Tuple is a collection of items stored in parentheses ().
1. count()
Definition: Counts how many times an element occurs in the tuple.
Example:
tuple_elements = (2, 3.4, 4.5, "java", 3.4, 45)
print(tuple_elements.count(3.4))
Output:
2
2. index()
Definition: Returns the index of the first occurrence of the specified value.
Example:
tuple_elements = (2, 3.4, 4.5, "java", 3.4, 45)
print(tuple_elements.index(3.4))
Output:
Note
Python keywords are reserved words and cannot be used as variable names, function names, or
file names.