Python's syntax is known for being clean, minimal, and easy to understand.
{}
to define code blocks.#
.Example:
# This is a comment
name = "Akash"
if name == "Akash":
print("Welcome Akash!") # Indentation is required
A variable is a name that holds a value.
In Python, you don't need to declare the type explicitly. Just assign:
x = 10
name = "Akash"
Naming Rules
_
)Name
≠ name
)if
, for
, etc.)1. Integer (int
)
age = 25
print(type(age)) # Output: <class 'int'>
2. Floating Point (float
)
pi = 3.14
print(type(pi)) # Output: <class 'float'>
3. String (str
)
greeting = "Hello"
print(type(greeting)) # Output: <class 'str'>
4. Boolean (bool
)
is_active = True
print(type(is_active)) # Output: <class 'bool'>
You can convert between types using:
int("5") # Converts string to int
float("2.5") # Converts string to float
str(100) # Converts int to string
bool(1) # Converts int to bool (1 = True)
Example:
x = "100"
y = int(x) + 50
print(y) # Output: 150
You don’t need to declare variable types:
a = 5 # int
a = "Hello" # now a string
This makes Python flexible, but you should still use consistent naming and typing.
Multiple assignments:
x, y, z = 1, 2, 3
Swap two variables:
a, b = 10, 20
a, b = b, a
Constants (by convention):
PI = 3.14 # all caps indicate constant
user_name
count
, user_email
Concept | Description |
---|---|
Variable | Name that stores a value |
Data types | int, float, str, bool |
Dynamic typing | Type changes as value changes |
Type casting | Converting between data types |
Syntax rules | Indentation-based, readable, clean |
1. Create a program that asks the user for their name and age, then prints:
Hello Akash! You are 25 years old.
2. Try assigning different data types to a variable and print their types using type()
.
Leave a Reply