2024-10-20 21:20
Status:ongoing
Tags:pythondata-typesoperatorscontrol-structures
Python Scripting
Subject: Introduction to Python
Data Types
flowchart TD
A[Python Data Types]
A --> B[Numeric Types]
B --> C[int]
B --> D[float]
B --> E[complex]
A --> F[Sequence Types]
F --> G[str]
F --> H[list]
F --> I[tuple]
A --> J[Mapping Type]
J --> K[dict]
A --> L[Set Types]
L --> M[set]
A --> N[Boolean Type]
N --> O[bool]
Numeric Types
- int:
intis short for Integer numbers.- Example:
my_int = 42
print(my_float)
# Output: 42- float:
- Represents floating-point numbers (numbers with a decimal point).
- Example:
my_float = 3.14
print(my_float)
# Output: 3.14- complex:
- Represents complex numbers, which have a real and an imaginary part.
- Example:
my_complex = 3 + 4j
print(my_complex)
# Output: (3+4j)
print(my_complex.real)
# Output: 3.0
print(my_complex.imag)
# Output: 4.0Sequence Types
- str:
stris short for strings, which are sequences of characters.- Example:
my_string = "Hello"
print(my_string)
# Output: Hello- list:
- An ordered collection of items that can contain a mix of data types. Lists are mutable and written in square brackets.
- Example:
my_list = [1, 2, 3, "Python"]
print(my_list)
# Output: [1, 2, 3, 'Python']- tuple:
- An ordered collection of items similar to a list, but tuples are immutable and written in parentheses.
- Example:
my_tuple = (1, 2, 3)
print(my_tuple)
# Output: (1, 2, 3)Mapping Type
- dict:
- A dictionary is an unordered collection of key-value pairs. Each key must be unique.
- Example:
my_dict = {"name": "Alice", "age": 30}
print(my_dict)
# Output: {'name': 'Alice', 'age': 30}Set Types
- set:
- An unordered collection of unique elements.
- Example:
my_set = {1, 2, 3}
print(my_set)
# Output: {1, 2, 3}Boolean Type
- bool:
- A boolean represents one of two values:
TrueorFalse. - Example:
- A boolean represents one of two values:
my_bool = True
print(my_bool)
# Output: TrueOperators
Python supports various types of operators:
-
Arithmetic Operators
+(addition),-(subtraction),*(multiplication),/(division)//(floor division),%(modulus),**(exponentiation)
-
Comparison Operators
==(equal to),!=(not equal to)>(greater than),<(less than),>=(greater than or equal to),<=(less than or equal to)
-
Logical Operators
and,or,not
-
Assignment Operators
=,+=,-=,*=,/=,%=,**=,//=
-
Identity Operators
is,is not
-
Membership Operators
in,not in
Control Structures
Control structures direct the flow of your program:
-
Conditional Statements
if condition: # code block elif another_condition: # code block else: # code block -
For Loops
for item in iterable: # code block -
While Loops
while condition: # code block -
Break and Continue
break: Exits the loopcontinue: Skips the rest of the current iteration and moves to the next
Next, we’ll explore Functions and Modules in Python to learn how to organize and reuse our code effectively.