Visit Website

Variables, Data Types, and Operators

Mastering Python Basics: A Beginner’s Guide to Variables, Data Types, and Operators

Python is one of the most beginner-friendly programming languages in the world. Whether you're stepping into coding for the first time or switching from another language, Python offers a clean syntax and powerful features.

In this blog, we'll walk through the fundamentals of Python, covering variables, data types, operators, expressions, and more , with clear explanations and examples.

Variables in Python

Variables are containers for storing data values. In Python, variables are references (or pointers) to objects stored in memory.

a = 5
print(a)             # Output: 5
print(type(a))       # Output: <class 'int'>

Variables do not require an explicit declaration of type — Python infers it automatically.

Identifiers and Naming Rules

Identifiers are names used for variables, functions, classes, etc. They:

  • Can include letters, numbers, and underscores.

  • Cannot start with a digit.

  • Are case-sensitive.

  • Cannot be a Python keyword.

Valid:

my_name = "Mausam"
country_1 = "Nepal"

Invalid:

123name = "Wrong"
&val = 20

Reserved Keywords

import keyword
print(keyword.kwlist)

These are words like if, else, class, import, etc. Don't use them as variable names.

Python Data Types

Python has built-in support for several data types:

Category Examples
Text str
Numeric int, float, complex
Sequence list, tuple, range
Mapping dict
Set set, frozenset
Boolean bool
Binary bytes, bytearray, memoryview
None NoneType

Example:

data = {"name": "Mausam", "country": "Nepal"}
print(type(data))  # <class 'dict'>

Python Operators

Operators are used to perform operations on variables and values.

1. Arithmetic Operators

x, y = 6, 2

print(x + y)   # 8
print(x ** y)  # 36 (exponentiation)
print(x // y)  # 3 (floor division)

2. Assignment Operators

x = 5
x += 3  # same as x = x + 3

3. Comparison Operators

x = 6
y = 2

print(x == y)  # False
print(x > y)   # True

4. Logical Operators

x = 2
y = -2

print(x > 0 and y < 0)  # True

5. Identity Operators

a = [1, 2]
b = [1, 2]
print(a is b)  # False (different memory)

6. Membership Operators

colors = ['red', 'green', 'blue']
print('red' in colors)        # True
print('yellow' not in colors) # True

7. Bitwise Operators

x = 0b1100
y = 0b1010

print(bin(x & y))  # 0b1000

Operator Precedence

Python follows a specific order when evaluating expressions:

** > +x, -x, ~x > *, /, %, // > +, - > <<, >> > & > ^ > | > comparisons > not > and > or

Expressions vs. Statements

Expression Statement
Produces a value Executes an action
3 + 58 x = 5, print(x)
Always returns a result May or may not return a value

Interactive vs Script Mode

  • Interactive Mode: Type and run commands one-by-one (e.g., in Python shell or Colab).

  • Script Mode: Write code in a .py file and execute the file.

Comments in Python

Used to explain and document your code.

  • Single-line comment:

# This is a comment
  • Multi-line comment:

"""
This is a multi-line comment.
Used for documentation.
"""

Post a Comment

Visit Website
Visit Website
Mausam Welcome to WhatsApp chat
Hello! How can we help you today?
Type here...