PYTHON-FUNDAMENTALS: Constants Variables aNd Data Types
{% raw %}
Variables
A variable in Python is a symbolic name that references or points to an object.
Once a variable is assigned a value, it can be used to refer to that value throughout the program.
Variables act as containers (eg: box) for storing data values. But ideally they are reference to a memory where the value is stored.
How to name a variable ?
When naming variables in Python, there are a few rules and best practices to follow:
- Start with a letter or an underscore: Variable names must begin with a letter (a-z, A-Z) or an underscore (_).
- Followed by letters, digits, or underscores: After the first character, you can use letters, digits (0-9), or underscores.
- Case-sensitive: Variable names are case-sensitive. For example,
myVariableandmyvariableare different variables. - Avoid Python keywords: Do not use Python reserved words or keywords as variable names (e.g.,
class,def,for,while).
Examples of Valid Variable Names:
my_variablevariable1_hidden_variableuserName
Examples of Invalid Variable Names:
1variable(starts with a digit)my-variable(contains a hyphen)for(a reserved keyword)
Assigning Values to Variables
In Python, the assignment operator = is used to assign values to variables. The syntax is straightforward: variable_name = value.
Examples:
# Assigning integer valueage = 25# Assigning string valuename = "John Doe"# Assigning float valueheight = 5.9# Assigning boolean valueis_student = True
Multiple Assignments
Python allows you to assign values to multiple variables in a single line. This can make your code more concise and readable.
Example:
# Assigning multiple variables in a single linea, b, c = 5, 10, 15# Swapping values of two variablesx, y = y, x
Unpacking Sequences
Python also supports unpacking sequences, such as lists or tuples, into variables. This feature is handy when working with collections of data.
Example:
# Unpacking a tupleperson = ("Alice", 30, "Engineer")name, age, profession = person# Unpacking a listnumbers = [1, 2, 3]one, two, three = numbersVariable Types
Python is a dynamically typed language, which means you don’t need to declare the type of a variable when assigning a value to it. The type is inferred at runtime based on the assigned value.
Example:
# Dynamic typingmy_variable = 10 # my_variable is an integermy_variable = "Hello" # my_variable is now a string
You can check the type of a variable using the type() function.
Example:
my_var = 42print(type(my_var)) # Output: <class 'int'>my_var = "Python"print(type(my_var)) # Output: <class 'str'>
Constants
In Python, constants are variables whose values are not meant to change. By convention, constants are typically written in all uppercase letters with underscores separating words.
Note: However, Python does not enforce this, so constants are not truly immutable.
# Defining a constantPI = 3.14159MAX_USERS = 100
Data Types
Data types are the different kinds of values that you can store and work with.
Just like in your home, you have different categories of items like clothes, books, and utensils, in Python, you have different categories of data.
1. Numeric Types
- Integer (
int): Whole numbers.

- Float (
float): Decimal numbers.

- Complex (
complex): Complex numbers.

2. Text Type
- String (
str): Sequence of characters.

3. Boolean Type
- Boolean (
bool): Represents True or False.

4. None Type
- NoneType: Represents the absence of a value

5. Sequence Types
- List (
list): Ordered, mutable collection

- Tuple (
tuple): Ordered, immutable collection.

- Range (
range): Sequence of numbers.

6. Mapping Type
- Dictionary (
dict): Unordered, mutable collection of key-value pairs.

7, Set Type
- Set (
set): Unordered collection of unique elements.

- Frozenset (
frozenset): Immutable set.

Checking Data Type
- Syntax:
type(variable_name)

Why Do Data Types Matter?
Data types are important because they tell Python what kind of operations you can perform with the data. For example:
- You can add, subtract, multiply, and divide numbers.
- You can join (concatenate) strings together.
- You can access, add, remove, and change items in lists.
- You can look up values by their keys in dictionaries.
Using the right data type helps your program run smoothly and prevents errors.
Exercises
Infographics
https://parottasalna.com/wp-content/uploads/2024/07/variables-constants-and-data-types-in-python.pdf
Quiz:
{% endraw %}