Python data types, expression, statement full notes
Python data types, expression, statement full notes !!!!
Certainly, I can provide you with an overview of Python data types, expressions, and statements.
## Python Data Types:
In Python, data types define the type of value that a variable can hold. Here are some common data types:
1. **Numeric Types:**
- `int`: Integers, e.g., 5, -10.
- `float`: Floating-point numbers, e.g., 3.14, -0.5.
2. **Sequence Types:**
- `str`: Strings, e.g., "Hello, World!"
- `list`: Lists, e.g., [1, 2, 3].
- `tuple`: Tuples, e.g., (1, 2, 3).
3. **Mapping Type:**
- `dict`: Dictionaries, e.g., {'key': 'value', 'name': 'John'}.
4. **Set Types:**
- `set`: Unordered collections of unique elements, e.g., {1, 2, 3}.
5. **Boolean Type:**
- `bool`: Represents True or False.
6. **None Type:**
- `None`: Represents the absence of a value.
## Expressions:
Expressions are combinations of values, operators, and function calls that can be evaluated to produce a value.
```python
# Examples of expressions
x = 5
y = 10
sum_result = x + y
length = len("Hello")
```
## Statements:
Statements are lines of code that perform actions or control the flow of a program. Python has several types of statements:
1. **Assignment Statement:**
- Assigns a value to a variable.
```python
x = 10
name = "Alice"
```
2. **Conditional Statements:**
- Used for decision-making.
```python
if x > 0:
print("Positive")
else:
print("Non-positive")
```
3. **Loop Statements:**
- Used for iteration.
```python
for i in range(5):
print(i)
while x > 0:
x -= 1
```
4. **Function Definition:**
- Defines a function for reuse.
```python
def greet(name):
print("Hello, " + name)
```
5. **Import Statement:**
- Imports modules or parts of modules.
```python
import math
from datetime import datetime
```
6. **Exception Handling:**
- Deals with errors during runtime.
```python
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
```
7. **Class Definition:**
- Defines a blueprint for creating objects.
```python
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
```
These are some of the key concepts related to Python data types, expressions, and statements. They form the building blocks of Python programming and enable you to create powerful and flexible applications.
Comments
Post a Comment