python programming language tutorial
python programming language tutorial
TO ADVANCE
BY TRAINPDEV
1. Introduction to Python
Resources
BY TRAINPDEV
1. Introduction to Python
Resources
BY TRAINPDEV
1. Introduction to Python
Resources
WEEK 1-2
Introduction to Python
*Operators*
• - Arithmetic Operators:
• - Addition: `a + b`
• - Subtraction: `a – b`
• - Multiplication: `a * b`
• - Division: `a / b`
• - Modulus (Remainder): `a % b`
• - Exponentiation: `a ** b`
- Comparison Operators:
- Equal: `a == b`
- Not Equal: `a != b`
- Logical Operators:
- And: `a and b`
- Or: `a or b`
- Not: `not a`
- Assignment Operators:
- Assign: `a = b`
*Control Structures*
• - Conditional Statements:
*Functions*
*Modules*
Here are examples, definitions, and code for each of the concepts I
mentioned earlier:
- *Code:*
```
```
* **Data Types:**
```python
Age = 30
Print(age) # Output: 30
```
```python
```
Height = 1.75
```
```python
```
```
isAdmin = True
```
**List:** An ordered collection of items, e.g., [1, 2, 3], [”a”, “b”, “c”],
etc.
```python
```
```python
```
```
## Operators
```python
Num1 = 5
Num2 = 3
Print(sum) # Output: 8
```
**Addition:** `a + b`
```python
```
Sum = 5 + 3
Print(sum) # Output: 8
```
**Subtraction:** `a – b`
```python
Difference = 5 – 3
Print(difference) # Output: 2
```
``` * **Multiplication:** `a * b`
```python
```
Product = 5 * 3
Print(product) # Output: 15
```
**Division:** `a / b`
```python
Quotient = 5 / 3
```
```python
```
Remainder = 5 % 3
Print(remainder) # Output: 2
```
**Exponentiation:** `a ** b`
```python
Result = 5 ** 3
```
```* **Comparison Operators:**
**Equal:** `a == b`
```python
```
Result = 5 == 3
```
**Not Equal:** `a != b`
```python
Result = 5 != 3
```
```python
```
Result = 5 > 3
```
Result = 5 < 3
```
```python
```
Result = 5 >= 3
```
```python
Result = 5 <= 3
```
**And:** `a and b`
```python
```
Print(result) # Outputs
Here is the full note:
Definition
Example
Code
```
```
Data Types
*Integers*
```
Age = 30
Print(age) # Output: 30
```
*Floats*
Height = 1.75
```
*Strings*
```
```
*Boolean*
```
isAdmin = True
```
*List*
An ordered collection of items, e.g., [1, 2, 3], [”a”, “b”, “c”], etc.
```
Fruits = [”apple”, “banana”, “cherry”]
```
*Tuple*
An ordered, immutable collection of items, e.g., (1, 2, 3), (”a”, “b”, “c”), etc.
```
Operators
## Definition
## Example
## Code
```python
Num1 = 5
Num2 = 3
Print(sum) # Output: 8
```
Arithmetic Operators
*Addition*
`a + b`
```
Sum = 5 + 3
Print(sum) # Output: 8
```
*Subtraction*
`a – b`
```
Difference = 5 – 3
Print(difference) # Output: 2
```
*Multiplication*
`a * b`
```
Product = 5 * 3
Print(product) # Output: 15
```
*Division*
`a / b`
```
Quotient = 5 / 3
```
*Modulus (Remainder)*
`a % b`
```
Remainder = 5 % 3
Print(remainder) # Output: 2
```
*Exponentiation*
`a ** b`
```
Result = 5 ** 3
WEEK 1-2
Introduction to Python
• - List (list): An ordered collection of items, e.g., [1, 2, 3], [”a”, “b”,
“c”], etc.
*Operators*
• - Arithmetic Operators:
• - Addition: `a + b`
• - Subtraction: `a - b`
• - Multiplication: `a * b`
• - Division: `a / b`
• - Modulus (Remainder): `a % b`
• - Exponentiation: `a ** b`
- Comparison Operators:
- Equal: `a == b`
- Not Equal: `a != b`
- Logical Operators:
- And: `a and b`
- Or: `a or b`
- Not: `not a`
- Assignment Operators:
- Assign: `a = b`
*Control Structures*
• - Conditional Statements:
• - Loops:
*Functions*
Here are examples, definitions, and code for each of the concepts I
mentioned earlier:
- *Code:*
```
```
```
* **Data Types:**
```python
age = 30
print(age) # Output: 30
```
```python
```
height = 1.75
```
```python
```
```python
```
isAdmin = True
```
* **List:** An ordered collection of items, e.g., [1, 2, 3], [”a”, “b”, “c”],
etc.
```python
```
```python
```
```
## Operators
* **Code:**
```python
num1 = 5
num2 = 3
print(sum) # Output: 8
```
* **Addition:** `a + b`
```python
```
sum = 5 + 3
print(sum) # Output: 8
```
* **Subtraction:** `a - b`
```python
difference = 5 - 3
print(difference) # Output: 2
```
``` * **Multiplication:** `a * b`
```python
```
product = 5 * 3
print(product) # Output: 15
```
* **Division:** `a / b`
```python
quotient = 5 / 3
```
```python
```
remainder = 5 % 3
print(remainder) # Output: 2
```
* **Exponentiation:** `a ** b`
```python
result = 5 ** 3
```
* **Equal:** `a == b`
```python
```
result = 5 == 3
print(result) # Output: False
```
* **Not Equal:** `a != b`
```python
result = 5 != 3
```
```python
```
result = 5 > 3
```
```python
result = 5 < 3
```
```
result = 5 >= 3
```
```python
result = 5 <= 3
```
* **And:** `a and b`
```python
```
print(result) # Outputs
Definition
Code
```
```
Data Types
*Integers*
```
age = 30
print(age) # Output: 30
```
*Floats*
```
height = 1.75
```
*Strings*
Sequences of characters, e.g., “hello”, ‘hello’, etc. Strings can be enclosed in
single quotes or double quotes.
```
```
*Boolean*
```
isAdmin = True
```
*List*
An ordered collection of items, e.g., [1, 2, 3], [”a”, “b”, “c”], etc.
```
```
*Tuple*
An ordered, immutable collection of items, e.g., (1, 2, 3), (”a”, “b”, “c”), etc.
```
Operators
## Definition
## Example
## Code
```python
num1 = 5
num2 = 3
print(sum) # Output: 8
```
Arithmetic Operators
*Addition*
`a + b`
```
sum = 5 + 3
print(sum) # Output: 8
```
*Subtraction*
`a - b`
```
difference = 5 - 3
print(difference) # Output: 2
```
*Multiplication*
`a * b`
```
product = 5 * 3
print(product) # Output: 15
```
*Division*
`a / b`
```
quotient = 5 / 3
```
*Modulus (Remainder)*
`a % b`
```
remainder = 5 % 3
print(remainder) # Output: 2
```
*Exponentiation*
`a ** b`
```
result = 5 ** 3
## Comparison Operators
### Equal
`a == b`
```python
result = 5 == 3
```
*Not Equal*
`a != b`
```
result = 5 != 3
*Greater Than*
`a > b`
```
result = 5 > 3
```
*Less Than*
`a < b`
```
result = 5 < 3
```
`a >= b`
```
result = 5 >= 3
```
```
result = 5 <= 3
## Logical Operators
### And
`a and b`
```python
```
*Or*
`a or b`
```
```
*Not*
`not a`
```
result = not 5 > 3
Control Structures
## Conditional Statements
### If Statement
```python
x=5
if x > 10:
else:
```
*If-Else Statement*
If-Else Statement
```
x=5
if x > 10:
else:
```
If-Elif-Else Statement
```
x=5
if x > 10:
elif x == 5:
else:
```
Loops
*For Loop*
```
print(fruit)
```
While Loop
x=0
while x < 5:
print(x)
x += 1
```
Functions
*Defining a Function*
```
def greet(name):
greet(”John”)
```
Modules
*Importing a Module*
`import module_name`
```
import math
print(math.pi)
```
Arrays
*Creating an Array*
```
print(scores)
```
*Defining a Class*
```
class Person:
self.name = name
self.age = age
def greet(self):
person.greet()
_Defining a Class_
```
class Person:
self.name = name
self.age = age
def greet(self):
person.greet()
```
Constructors
_Defining a Constructor_
```
class Person:
self.name = name
self.age = age
person = Person(”John”, 30)
print(person.name)
print(person.age)
```
Methods
_Defining a Method_
```
class Person:
self.name = name
self.age = age
def greet(self):
person.greet()
```
Inheritance
_Defining a Subclass_
class Animal:
self.name = name
def eat(self):
print(self.name + “ is eating.”)
class Dog(Animal):
super().__init__(name)
self.breed = breed
def bark(self):
dog.eat()
dog.bark()
```
Polymorphism
_Method Overriding_
```
class Animal:
self.name = name
def sound(self):
class Dog(Animal):
super().__init__(name)
self.breed = breed
def sound(self):
dog.sound()
```
_Method Overloading_
```
class Calculator:
if len(args) == 1:
return args[0] ** 2
elif len(args) == 2:
else:
calculator = Calculator()
print(calculator.calculate(5)) # Output: 25
```
Encapsulation
_Public Attributes_
`attribute_name = value`
```
class Person:
self.name = name
self.age = age
print(person.name)
print(person.age)
```
_Private Attributes_
`__attribute_name = value`
```
class Person:
self.__name = name
self.__age = age
def get_name(self):
return self.__name
def get_age(self):
return self.__age
print(person.get_name())
print(person.get_age())
```
Abstraction
_Abstract Classes_
```
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Square(Shape):
self.side = side
def area(self):
return self.side ** 2
square = Square(5)
print(square.area())
TEST
b. Operators
b. Control Structures
b. Functions
b. Modules
b. Arrays
b. Encapsulation