Python Basics: Variables, Data Types, and Basic Operators

aishwarya_raj_978520e6399

Aishwarya Raj

Posted on November 4, 2024

Python Basics: Variables, Data Types, and Basic Operators

Starting Simple: Why Variables Matter

In Python, variables are containers with labels that hold whatever data you want—text, numbers, lists, you name it. You don’t need to tell Python what type of data is in there; it just goes with the flow.

Example:

name = "Alice"
age = 25
height = 5.5
Enter fullscreen mode Exit fullscreen mode

Here, name is a string, age is an integer, and height is a float. Now you can store info without getting bogged down in type declarations.


Diving into Data Types

Python data types cover pretty much all your needs:

  • Integer (age = 25): Whole numbers.
  • Float (height = 5.5): Numbers with decimals.
  • String (name = "Alice"): A sequence of characters.
  • Boolean (is_active = True): True or False.
  • Lists, Tuples, Sets, and Dictionaries: Collections of items—like keeping a bunch of things in separate, labeled boxes.

Operators: Simple Calculations, Big Impact

You’ve got your arithmetic operators (+, -, *, /), but Python’s also got some extras:

Examples:

a = 10
b = 3

print(a + b)  # Addition
print(a ** b) # Exponentiation (fancy word for power)
print(a // b) # Floor division (cuts off decimals)
Enter fullscreen mode Exit fullscreen mode

Real-World Example: Checking Access

Here’s a quick profile check: if the age falls between 18 and 60 and the user’s an admin, they get access.

age = 30
is_admin = True

if 18 <= age <= 60 and is_admin:
    print("Access Granted")
else:
    print("Access Denied")
Enter fullscreen mode Exit fullscreen mode

There you go!

As a wise coder once said, "May your bugs be few and your syntax be clean."

💖 💪 🙅 🚩
aishwarya_raj_978520e6399
Aishwarya Raj

Posted on November 4, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related