Variables in Python: A Comprehensive Guide

Variables in Python: A Comprehensive Guide

Variables are fundamental elements in programming used to store data that can be referenced and manipulated in a program. In Python, variables are created when you assign a value to them, and they do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable.


Declaring and Assigning Variables

In Python, assigning a value to a variable is simple and intuitive:

age = 22
height = 5.7
name = "Sazid"

To print the values stored in the variables, you can use the print() function:

print("Age:", age)
print("Height:", height)
print("Name:", name)
#Age: 22
#Height: 5.7
#Name: Sazid

Naming Conventions

When naming variables in Python, follow these rules to maintain readability and avoid errors:

  • Variable names must start with a letter or an underscore (_).

  • They can contain letters, numbers, and underscores.

  • Variable names are case-sensitive.

  • Use descriptive names to make your code more readable.

Examples of Valid Variable Names:

first_name = "MI"
last_name = "Sazid"

Examples of Invalid Variable Names:

# 2age = 30  # Cannot start with a number
# first-name = "Sazid"  # Hyphens are not allowed
# @name = "Sazid"  # Special characters are not allowed

Case Sensitivity in Variables:

name = "MI"
Name = "SAzid"
# These are two distinct variables due to case sensitivity.

Understanding Variable Types

Python is dynamically typed, meaning the type of a variable is determined at runtime. Here are some common types:

age = 22  # int
height = 5.7  # float
name = "Sazid"  # str
is_student = True  # bool

Type Checking and Conversion

You can check a variable’s type using the type() function. Python also allows type conversion when needed:

Type Checking:

age = 22
height = 5.7
print(type(age))  # Output: <class 'int'>
print(type(height)) # Output: <class 'float'>

Type Conversion:

age_str = str(age)  # Convert int to string
print(age_str)
print(type(age_str))

# age = '22'
# Output: <class 'str'>

print(type(int(age)))  # Convert string to int

However, some conversions may lead to errors:

name = "Sazid"
int(name)  # This will raise a ValueError

Dynamic Typing

Python allows variables to change types as the program executes. This is called dynamic typing.

var=10 #int
print(var,type(var))

var="Hello"
print(var,type(var))

var=3.14
print(var,type(var))

# Output: 10 <class 'int'>
# Output: Hello <class 'str'>
# Output: 3.14 <class 'float'>

Getting Input from the User

You can use the input() function to get user input, which is always returned as a string. Convert it to the desired type as needed:

age = int(input("What is your age? "))
print(age, type(age))

A Simple Calculator Example

Here’s an example that demonstrates how to use variables and user input to build a basic calculator:

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

sum = num1 + num2
difference = num1 - num2
product = num1 * num2
quotient = num1 / num2

print("Sum:", sum)
print("Difference:", difference)
print("Product:", product)
print("Quotient:", quotient)

Conclusion

Variables are essential in Python programming for storing and manipulating data. Understanding how to declare, assign, and use variables effectively is crucial for writing functional and efficient code. Following proper naming conventions and understanding variable types will help in maintaining readability and consistency in your code.

©️Learn from udemy