Basic Topics of Python
1. Variables and Data Types
Variables are used to store data. Python has several basic data types:
- int: Whole numbers
- float: Numbers with decimals
- str: Text
- bool: True or False
Example:
x=5
y = 3.2
name = "Alice"
is_active = True
Output:
x is of type <class 'int'>
y is of type <class 'float'>
name is of type <class 'str'>
is_active is of type <class 'bool'>
2. Operators
Operators are used to perform operations on variables and values.
Example:
a = 10
b=3
print(a + b) # Addition
print(a > b) # Comparison
print(a == b or a > b) # Logical
Output:
13
True
True
3. Control Flow
Used to control the flow of a program using conditions and loops.
Example:
x=7
if x > 5:
print("Greater than 5")
else:
print("5 or less")
Output:
Greater than 5
4. Functions
Functions are blocks of reusable code.
Example:
def greet(name):
return "Hello " + name
print(greet("Bob"))
Output:
Hello Bob
5. Data Structures
Used to store collections of data.
Examples:
my_list = [1, 2, 3]
my_tuple = (1, 2, 3)
my_dict = {"a": 1, "b": 2}
my_set = {1, 2, 3}
Output:
List: [1, 2, 3]
Tuple: (1, 2, 3)
Dict: {'a': 1, 'b': 2}
Set: {1, 2, 3}
6. String Manipulation
Strings can be modified or accessed in parts.
Example:
text = "Hello"
print([Link]())
print(text[1:4])
Output:
HELLO
ell