Variables in Python

Variables are one of the first concepts every Python programmer learns. A variable stores a value that your program can use and update throughout its execution. In Python, you create a variable simply by assigning a value—there is no need to declare its data type beforehand because Python automatically determines it.

Characteristics of Variables in Python

  1. Dynamic Typing: Variables can store different data types, and their types can change at runtime
  2. No Declaration Required: No need to declare the type of a variable before assigning a value to it.
  3. Case-Sensitive: Variable names are case-sensitive (age & Age are different variables).
  4. Naming Rules:
    • Must start with a letter or an underscore (_).
    • Can contain letters, numbers, and underscores (e.g., my_variable_23).
    • Cannot start with a number.
    • Cannot use Python keywords as variable names (e.g., if, while).

Assigning Values to Variables

You can assign a value to a variable(s) using the “  =  “ operator.

# Assigning an integer
age = 25

# Assigning a floating-point number
height = 6.1

# Assigning a string
name = "Ravi"

# Assigning a boolean
is_student = True

Multiple Assignments

Python allows assigning a single value to multiple variables or multiple values to multiple variables in a line.

# Assigning the same value to multiple variables
x = y = z = 20

# Assigning multiple values to multiple variables
a, b, c = 10, 20, 30

Variable Re-initialization

Variables in Python can be reinitialized with a new value or even a new type at any point in the code. This is made possible due to Python’s dynamic typing.

# Initial value
count = 10
print(count)  # Output: 10

# Reinitializing with a new value
count = 20
print(count)  # Output: 20

# Reinitializing with a different type
count = "Twenty"
print(count)  # Output: "Twenty"

Variable Types and Dynamic Typing

In Python, the type of a variable differs from the value assigned to it, allowing variables to change data type dynamically.

# Integer
num = 1
print(type(num))  # Output: <class 'int'>

# Changing the type to a string
num = "One"
print(type(num))  # Output: <class 'str'>

Basic Variable Operations

Variables can be used in various operations, including arithmetic and string concatenation.

# Arithmetic operations
x = 5
y = 10
sum = x + y  # Output: 15

# String concatenation (str+str)
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name  # Output: "John Doe"

Constants in Python

Python doesn’t have a built-in constant data type. However, by convention, variables intended to be constants are named using all uppercase letters.

PI = 3.14159
GRAVITY = 9.81

While these values can technically be changed, using all uppercase letters indicates that they should not be modified (as in real-time).

Frequently Asked Questions

What is a variable in Python?

A variable is a name that refers to a value stored in memory. It allows you to store and reuse data throughout your program.

Do I need to declare variable types in Python?

No. Python automatically determines the variable’s type based on the assigned value.

What is the best naming convention for Python variables?

Use descriptive names written in snake_case, such as student_name or total_marks.

Can a variable change its data type?

Yes. Python supports dynamic typing, so the same variable can hold different types of values during execution.

Conclusion

Python variables are the building blocks of every Python program. They allow you to store, update, and manage data efficiently without explicitly declaring data types. By following proper naming conventions, understanding dynamic typing, and using variables effectively, you can write cleaner, more maintainable, and professional Python code. Practice creating and using variables regularly to build a strong foundation for advanced Python concepts.

Knowledge Check

Related Article No.4

Data Analytics with Power Bi and Fabric
Could Data Engineer
Data Analytics With Power Bi Fabic
AWS Data Engineering with Snowflake
Azure Data Engineering
Azure & Fabric for Power bi
Full Stack Power Bi
Subscribe to our channel & Don’t miss any update on trending technologies

Kick Start Your Career With Our Data Job

Master Fullstack Power BI – SQL, Power BI, Azure Cloud & Fabric Tools
Master in Data Science With Generative AI Transform Data into Business Solutions
Master Azure Data Engineering – Build Scalable Solutions for Big Data
Master AWS Data Engineering with Snowflake: Build Scalable Data Solutions
Transform Your Productivity With Low Code Technology: Master the Microsoft Power Platform

Social Media channels

► KSR Datavizon Website :- https://www.datavizon.com
► KSR Datavizon LinkedIn :- https://www.linkedin.com/company/datavizon/
► KSR Datavizon You tube :- https://www.youtube.com/c/KSRDatavizon
► KSR Datavizon Twitter :- https://twitter.com/ksrdatavizon
► KSR Datavizon Instagram :- https://www.instagram.com/ksr_datavision
► KSR Datavizon Face book :- https://www.facebook.com/KSRConsultingServices
► KSR Datavizon Playstore :- https://play.google.com/store/apps/details?id=com.datavizon.courses&hl=en-IN
► KSR Datavizon Appstore :- https://apps.apple.com/in/app/ksr-datavizon/id1611034268

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *