AI NOTES
PYTHON
1. Introduction to Programming Languages:
● Definition of Programming Language:
● A formal language that prescribes a set of instructions for producing specific
outputs.
● Characteristics of Programming Languages:
● Vocabulary: Set of terms and symbols.
● Grammatical Rules: Syntax defining the structure of programs.
● Common Programming Languages:
● BASIC, Pascal, C, C++, Java, Haskell, Ruby, Python.
2. Introduction to Python:
● Python Overview:
● Cross-platform programming language.
● Downloading and Setting up Python:
● Python is cross-platform, supporting Windows, MacOS, Linux, and more.
● Installation steps: Download Python from python.org, select OS, and follow
installation steps.
● Applications of Python:
● Web and Internet Development.
● Business Applications.
● Games and 3D Graphics.
● Database Access.
● Software Development.
● Desktop GUI Applications.
3. Python Integrated Development Environment (IDE):
● Installing Python using Python IDLE:
● Python IDLE (Integrated Development Environment) is installed along with
Python.
● Provides an interface for coding and running Python programs.
● Running Python in Interactive and Script Mode:
● Interactive mode for quick testing and single commands.
● Script mode for writing and executing complete programs.
● IDLE Features:
● Editing, running, browsing, and debugging Python programs in a single interface.
4. Python Basic Operations:
● Arithmetic Operators:
● + Addition, - Subtraction, Multiplication, / Division, // Integer Division, %
Remainder, Exponentiation.
● Comparison Operators:
● > Greater Than, < Less Than, == Equal To, != Not Equal To, >= Greater Than or
Equal To, <= Less Than or Equal To.
● Logical Operators:
● and Logical AND, or Logical OR, not Logical NOT.
● Assignment Operators:
● = Assignment, += Add and Assign, -= Subtract and Assign, = Multiply and Assign,
/= Divide and Assign.
5. Python Input and Output:
● Using print() for Output:
● Displaying output on the console.
● Taking User Input with input():
● Collecting user input during program execution.
● Type Conversion:
● Converting data types using int(), float(), str().
6. Python Variables and Constants:
● Variables:
● Named memory locations holding data.
● Constants:
● Immutable values that don't change during program execution.
7. Python Data Types:
● Numeric Types:
● int for integers, float for floating-point numbers.
● Boolean Type:
● bool representing True or False.
● Sequence Types:
● str for strings, list for lists, tuple for tuples.
● Set and Mapping Types:
● set for sets, dict for dictionaries.
8. Python Type Conversion:
● Implicit Type Conversion:
● Automatic conversion by Python based on context.
● Explicit Type Conversion:
● Using functions like str(), int(), float() for manual conversion.
9. Python Operators (Comparison and Logical):
● Comparison Operators:
● >, <, ==, !=, >=, <=.
● Logical Operators:
● and, or, not.
10. Python Assignment Operators:
- =, +=, -=, =, /=.
11. Practical Code Applications:
1. Calculating Area and Perimeter of a Rectangle:
```python
# User input for length and breadth
length = float(input("Enter the length of the rectangle: "))
breadth = float(input("Enter the breadth of the rectangle: "))
# Calculations
area_rectangle = length breadth
perimeter_rectangle = 2 (length + breadth)
# Output
print("Area of the rectangle:", area_rectangle)
print("Perimeter of the rectangle:", perimeter_rectangle)
```
2. Calculating Area of a Triangle:
```python
# User input for base and height
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
# Calculation
area_triangle = 0.5 base height
# Output
print("Area of the triangle:", area_triangle)
```
12. Summary:
● Key Features of Python:
● Cross-platform, easy syntax, high-level, dynamically typed.
● Different Coding Modes:
● Interactive and Script Mode.
● Comments in Python:
● Using # for single-line comments, '''...''' for multi-line comments.
● Identifier Properties and Naming Rules:
● Case-sensitive, meaningful names, no special symbols.
● Python Input and Output:
● print() for output, input() for user input.
● Type Conversion:
● Implicit and Explicit.
● Variables and Constants:
● Named memory locations and immutable values.
● Python Data Types:
● Numeric, Boolean, Sequence, Set, Mapping.
● Python Operators:
● Arithmetic, Comparison, Logical, Assignment.
LISTS AND TUPLES
Introduction to Lists:
● A list in Python is a sequence of values of any type, enclosed in square brackets.
● Elements or items are the values within the list.
● Lists can contain elements of different types, including integers, floats, strings, and even
other lists (nested lists).
● Lists are versatile and widely used in Python.
Creating a List:
● In Python, a list is created using square brackets [].
● Examples:
● Empty list: empty_list = []
● List of integers: age = [15, 12, 18]
● List with mixed data types: student_height_weight = ["Ansh", 5.7, 60]
● Nested list: student_marks = ["Aditya", "10-A", ["english", 75]]
Accessing Elements of a List:
● Two ways to access elements:
● List Index: Index starts from 0; positive index to access elements from the
beginning.
● Negative Indexing: -1 refers to the last item; negative index to access elements from
the end.
● Examples:
● Using List Index: print(my_list[0]), print(my_list[4])
● Accessing Value in a nested list: print(n_list[0][1]), print(n_list[1][3])
● Using Negative Index: print(day[-1]), print(day[-6])
Adding Elements to a List:
● Three methods:
● Using append() method to add elements or lists to the end.
● Using insert() method to add elements at a specific position.
● Using extend() method to add multiple elements at the end.
● Examples:
● Using append(): List.append(1), List.append(2), List.append(4)
● Using insert(): List.insert(3, 12), List.insert(0, 'Kabir')
● Using extend(): List.extend([8, 'Artificial', 'Intelligence'])
Removing Elements from a List:
● Two methods:
● Using remove() method to remove a specific element.
● Using pop() method to remove and return an element based on index.
● Examples:
● Using remove(): List.remove(5), List.remove(6)
● Using pop(): List.pop(), List.pop(2)
Slicing of a List:
● Slicing allows printing a specific range of elements from a list.
● Different slice operations: List[start:stop], List[:stop], List[start:], List[:], List[::-1] for
reverse.
● Examples:
● Slicing elements in a range: List[3:8]
● Elements sliced till the end: List[5:]
● Elements sliced in reverse: List[::-1]
Practical Code Applications:
1. Accessing Elements using List Index:
```python
# Accessing Elements using List Index
my_list = ['p', 'y', 't', 'h', 'o', 'n']
print("First element:", my_list[0]) # Output: p
print("Fifth element:", my_list[4]) # Output: o
```
2. Accessing Value in a Nested List:
```python
# Accessing Value in a Nested List
nested_list = ["Happy", [2, 0, 1, 5]]
print("Element at index 0, position 1:", nested_list[0][1]) # Output: a
print("Element at index 1, position 3:", nested_list[1][3]) # Output: 5
```
3. Adding Elements using append(), insert(), and extend():
```python
# Adding Elements using append(), insert(), and extend()
my_list = [1, 2, 3, 4]
# Using append()
my_list.append(5)
print("After append(5):", my_list) # Output: [1, 2, 3, 4, 5]
# Using insert()
my_list.insert(2, 6)
print("After insert(2, 6):", my_list) # Output: [1, 2, 6, 3, 4, 5]
# Using extend()
my_list.extend([7, 8, 9])
print("After extend([7, 8, 9]):", my_list) # Output: [1, 2, 6, 3, 4, 5, 7, 8, 9]
```
4. Removing Elements using remove() and pop():
```python
# Removing Elements using remove() and pop()
my_list = [1, 2, 3, 4, 5]
# Using remove()
my_list.remove(3)
print("After remove(3):", my_list) # Output: [1, 2, 4, 5]
# Using pop()
popped_element = my_list.pop()
print("After pop():", my_list) # Output: [1, 2, 4]
print("Popped element:", popped_element) # Output: 5
```
5. Slicing a List in various ways:
```python
# Slicing a List in various ways
my_list = ['G', 'O', 'O', 'D', 'M', 'O', 'R', 'N', 'I', 'N', 'G']
# Slicing elements in a range
sliced_list = my_list[3:8]
print("Sliced elements in a range 3-8:", sliced_list) # Output: ['D', 'M', 'O', 'R', 'N']
# Elements sliced till the 6th element from last
sliced_list = my_list[:-6]
print("Elements sliced till 6th element from last:", sliced_list) # Output: ['G', 'O', 'O', 'D',
'M', 'O']
# Elements sliced from index -6 to -1
sliced_list = my_list[-6:-1]
print("Elements sliced from index -6 to -1:", sliced_list) # Output: ['R', 'N', 'I', 'N', 'G']
# Printing List in reverse
sliced_list = my_list[::-1]
print("Printing List in reverse:", sliced_list) # Output: ['G', 'N', 'I', 'N', 'R', 'O', 'M', 'D',
'O', 'O', 'G']
```
Summary:
● Lists are versatile and can contain elements of different types.
● Creation, access, addition, and removal of elements are essential list operations.
● Slicing allows extracting specific ranges or reversing a list.