Basic Python Syntax and Data Types 📝: Writing Your First Code
Understanding Python syntax and data types is fundamental. Python uses indentation to define code blocks, simplifying readability.
Hello World!
print('Hello, World!')
This prints a simple message to the console.
Variables and Data Types:
- Integers:
x = 5
- Floats:
pi = 3.14
- Strings:
name = 'Alice'
- Booleans:
is_valid = True
Type Checking:
type(x) # Output: <class 'int'>
Comments: Use #
for single-line comments.
# This initializes an age variable
age = 30
Basic Operations:
sum = 10 + 5
diff = 10 - 5
prod = 10 * 5
quot = 10 / 5 # Always float division
# Integer division
div_int = 10 // 3 # Result: 3
# Modulo (remainder)
remainder = 10 % 3 # Result: 1
Mastering syntax and data types is crucial to write effective Python code that is clear and efficient.