Python Lab1 Extension
Python Lab1 Extension
Python Basics
*Data Types*
Python has several built-in data types, including:
```
Examples of data types
my_int = 10
my_float = 3.14
my_complex = 3 + 4j
my_bool = True
my_str = "Hello, World!"
```
my_var = 10
print(id(my_var)) # Output: unique identifier
print(type(my_var)) # Output: <class 'int'>
```
*range()*
- `range()`: generates a sequence of numbers
```
for i in range(5):
print(i) # Output: 0, 1, 2, 3, 4
```
*Base/Type Conversion*
- `int()`: converts a value to an integer
- `float()`: converts a value to a floating point number
- `str()`: converts a value to a string
```
my_str = "10"
my_int = int(my_str)
print(my_int) # Output: 10
my_float = 3.14
my_str = str(my_float)
print(my_str) # Output: "3.14"
```
2. Operators
*Arithmetic Operators*
- `+`: addition
- `-`: subtraction
- `*`: multiplication
- `/`: division
- `//`: floor division
- `%`: modulus
- `**`: exponentiation
```
a = 10
b=3
print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333333333333335
```
*Relational Operators*
- `==`: equal to
- `!=`: not equal to
- `>`: greater than
- `<`: less than
- `>=`: greater than or equal to
- `<=`: less than or equal to
```
a = 10
b=5
print(a > b) # Output: True
print(a < b) # Output: False
```
*Logical Operators*
- `and`: logical and
- `or`: logical or
- `not`: logical not
```
a = True
b = False
print(a and b) # Output: False
print(a or b) # Output: True
print(not a) # Output: False
```
*Bitwise Operators*
- `&`: bitwise and
- `|`: bitwise or
- `^`: bitwise xor
- `~`: bitwise not
```
a = 5 # 101
b = 3 # 011
print(a & b) # Output: 1 (001)
print(a | b) # Output: 7 (111)
```
*Membership Operators*
- `in`: membership test
- `not in`: non-membership test
```
my_list = [1, 2, 3]
print(2 in my_list) # Output: True
print(4 in my_list) # Output: False
```
*Identity Operators*
- `is`: identity test
- `is not`: non-identity test
```
a = 10
b = 10
print(a is b) # Output: True
```
*Ternary Operator*
- `value_if_true if condition else value_if_false`
```
age = 25
status = "adult" if age >= 18 else
```