Home Coding & Scripting Python Variables, Data Types, and Operators

Python Variables, Data Types, and Operators

287
0

Learning Python starts with three simple building blocks: variables, data types, and operators. These are the basics you need before moving to functions, loops, or more advanced topics. Let’s go step by step.


📝 If You Want to Practice the Code

If you plan to practice the Python code mentioned within this blog post, you’ll need a working Python environment. You can execute the commands in one of the following ways (choose only one option):

👉 Choose whichever environment you’re most comfortable with. If you haven’t set up Python yet, start with one of the installation posts before trying the examples here.


🔹 Variables

A variable is like a container that stores information for later use.
In Python, you don’t need to declare the type — it’s decided automatically.

# Examples of variables
name = "Alice"      # A string
age = 25            # An integer
height = 5.6        # A float
is_student = True   # A boolean

👉 Think of variables as “labels” pointing to values.


🔹 Data Types

The most common data types in Python are:

  1. Numbers
    • int → whole numbers (10, -5)
    • float → decimals (3.14, -0.5)
  2. Strings
    • Text inside quotes: "Hello" or 'World'
    • Strings can be combined or sliced: greeting = "Hello" print(greeting + " World") # Hello World print(greeting[0]) # H
  3. Booleans
    • Either True or False
    • Often used in conditions: is_raining = False print(is_raining) # False
  4. None
    • Represents “nothing” or empty: data = None

🔹 Operators

Operators let you perform actions on variables and values.

1. Arithmetic Operators

a = 10
b = 3

print(a + b)  # Addition → 13
print(a - b)  # Subtraction → 7
print(a * b)  # Multiplication → 30
print(a / b)  # Division → 3.333...
print(a // b) # Floor division → 3
print(a % b)  # Modulus (remainder) → 1
print(a ** b) # Exponent → 1000

2. Comparison Operators

print(a == b)  # Equal → False
print(a != b)  # Not equal → True
print(a > b)   # Greater than → True

3. Logical Operators

x = True
y = False

print(x and y)  # False
print(x or y)   # True
print(not x)    # False

4. Assignment Operators

c = 5
c += 2  # same as c = c + 2
print(c)  # 7

📝 Mini Task

Try this small exercise:

# Store your name, age, and height in variables.
# Print them in a single sentence.

name = "Alice"
age = 25
height = 5.6

print("My name is", name, ", I am", age, "years old, and my height is", height, "meters.")

👉 Change the values and re-run to see how variables make your code flexible.


✅ Summary

  • Variables are names that store values.
  • Data types define what kind of value is stored (numbers, strings, booleans, etc.).
  • Operators let you do math, compare values, and combine logic.
Previous articleVirtual Environment in Python and How to Set One Up
Next articleControl Flow in Python (if/else, for, while)
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here