Language syntax

1. Input and Output
input()
Takes input from the user as a string.
print()
Displays output on the screen.
Example:
2. Variables and Data Types**
int
Integer numbers
x = 10
float
Floating-point numbers
pi = 3.14
complex
Complex numbers
z = 2 + 3j
str
String (text)
name = "Alice"
str
String (text)
name = "Bob"
str
String (text)
name = 'Bob'
str
String & Multiline string (text)
name = """Bob"""
str
String & Multiline string(text)
name = '''Bob'''
bool
Boolean value: True or False
is_valid = True
list
Ordered, mutable sequence
fruits = ["apple", "banana"]
tuple
Ordered, immutable sequence
coordinates = (10, 20)
range
Sequence of numbers
r = range(5)
dict
Key-value pairs
person = {"name": "Alice", "age": 25}
set
Unordered collection of unique items
colors = {"red", "green"}
frozenset
Immutable version of set
frozen = frozenset([1, 2, 3])
bytes
Immutable sequence of bytes
b = b"hello"
bytearray
Mutable sequence of bytes
ba = bytearray(5)
memoryview
View over binary data
mv = memoryview(bytes(5))
NoneType
Represents the absence of a value
value = None
Python manages memory automatically (no malloc/free like C). Garbage collection is built-in.
2.1. Main cast functions
3. Control Structures
4. Loops
5. Functions
5.1. Type hints:
6. File Handling
open()
Opens a file. Modes: "r", "w", "a", etc.
.read(), .readlines(), .write()
Reads from/writes to file.
.close()
Closes the file. (Not needed if using with statement).
When the program exits from the with block, the f.close() is automatically called saving also the file content.
7. Object-Oriented Programming
Example:
10. Main Function (Optional in Python)
if __name__ == "__main__":
Python runs code here only if script is executed directly (not imported).
If you run the file with this instruction, the interpreter will execute the code inside the code block:
If you import the file in another python script, the "if" code block will not be executed.
Example:
Last updated