Python syntax is clean and easy to understand, making it a great language for beginners. Let’s break down the core concepts:
1. Printing Output
The print()
function is used to display output in Python.
print("Hello, World!")
2. Comments in Python
Comments help explain the code and are ignored by the Python interpreter. Use the #
symbol for single-line comments.
# This is a comment
print("Comments are not executed!")
3. Variables and Data Types
- Variables store data and can be changed during program execution.
- You don’t need to declare data types explicitly; Python assigns types automatically.
Example:
name = "Chandra" # String
age = 30 # Integer
height = 5.8 # Float
is_student = True # Boolean
4. Basic Data Types in Python
- int: Whole numbers (e.g.,
1
,100
,-20
) - float: Numbers with decimals (e.g.,
3.14
,-0.5
) - str: Text or strings (e.g.,
"Hello"
,"Python"
) - bool: Boolean values (
True
orFalse
)
Example:
x = 5 # int
y = 3.14 # float
z = "Python" # str
x = 5 # int
y = 3.14 # float
z = “Python” # str
a = 10
b = 3
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
6. Type Conversion
You can convert between different data types using built-in functions like int()
, float()
, str()
.
num_str = "123"
num_int = int(num_str) # Convert string to integer
print(num_int)
These fundamentals will be the building blocks for writing Python programs!