Document
Document
Python has several built-in data types used to store and manipulate different kinds of data. These data types are
categorized into the following main types:
1. Numeric Types
int: Integer numbers (e.g., 5, -3, 100)
float: Floating-point numbers (e.g., 3.14, -0.001)
complex: Complex numbers (e.g., 3+5j)
2. Sequence Types
str: String of characters (e.g., "Hello", 'Python')
list: Ordered, mutable collection (e.g., [1, 2, 3])
tuple: Ordered, immutable collection (e.g., (1, 2, 3))
3. Set Types
set: Unordered, mutable collection of unique items (e.g., {1, 2, 3})
frozenset: Immutable version of a set
4. Mapping Type
dict: A collection of key-value pairs (e.g., {"name": "Alice", "age": 25})
5. Boolean Type
bool: Represents True or False values
6. None Type
None: Represents the absence of a value or a null value
2. Global Scope
A variable declared outside all functions is a global variable.
It is accessible throughout the program, including inside functions (if not shadowed).
Example:
x = 10 # Global variable
def show():
print(x)
3. Writing to a File
Write(): Writes a string to a file
Writelines(): Writes a list of strings
Example:
File = open(“data.txt”, “w”)
File.write(“Hello, World!”)
File.close()
4. Closing a File
Always close the file using file.close() to free system resources.
Conditional Statements
These allow the program to make decisions using if, elif, and else.
Example:
X = 10
If x > 0:
Print(“Positive”)
Elif x == 0:
Print(“Zero”)
Else:
Print(“Negative”)
Looping Statements
Used for repeating a block of code multiple times.
For loop: Iterates over a sequence (list, tuple, string, etc.)
For I in range(5):
Print(i)
While loop: Repeats as long as a condition is true
I=0
While I < 5:
Print(i)
I += 1
Jumping Statements
Used to alter the normal flow inside loops.
Break: Exits the loop completely
For I in range(10):
If I == 5:
Break
Print(i)
Continue: Skips the current iteration and moves to the next
For I in range(5):
If I == 2:
Continue
Print(i)
For I in range(5):
If I == 3:
Pass
Print(i)