Python Syntax, Variables and Data Types

August 01, 2025   4 Min Read 30

What You’ll Learn in This Blog

  • Python syntax rules
  • How to declare and use variables
  • Core data types: int, float, str, bool
  • Type casting and dynamic typing
  • Best practices with examples

What is Python Syntax?

Python's syntax is known for being clean, minimal, and easy to understand.

Basic Rules

  • Python uses indentation instead of braces {} to define code blocks.
  • A line of code ends with a newline (not semicolons like in C/C++).
  • Comments start with #.

Example:

# This is a comment
name = "Akash"
if name == "Akash":
    print("Welcome Akash!")  # Indentation is required

Variables in Python

What is a Variable?

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

  • Can contain letters, digits, and underscores (_)
  • Must start with a letter or underscore
  • Case-sensitive (Namename)
  • Don't use Python keywords (if, for, etc.)

Python Data Types

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'>

Type Casting in Python

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

Python is Dynamically Typed

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.

Python Variable Tricks

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

Best Practices

  • Use snake_case for variable names: user_name
  • Use descriptive names: count, user_email
  • Comment where necessary
  • Stick to one naming style across your project

Summary

ConceptDescription
VariableName that stores a value
Data typesint, float, str, bool
Dynamic typingType changes as value changes
Type castingConverting between data types
Syntax rulesIndentation-based, readable, clean

Task

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