3.PYTHON-FUNDAMENTALS: CONSTANTS VARIABLES AND DATA TYPES
Tags: python, programming, beginners
Published: 2024-07-11 07:26:53

PYTHON-FUNDAMENTALS:
 **constants variables and data Types**

**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, myVariable and myvariable are 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_variable
 variable1
 _hidden_variable
 userName`
**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 value
`age = 25`
 
# Assigning string value
`name = "John Doe"`
 
# Assigning float value
`height = 5.9`
 
# Assigning boolean value
`is_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 line
`a, b, c = 5, 10, 15`
 
# Swapping values of two variables
`x, 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 tuple
`person = ("Alice", 30, "Engineer")`
name, age, profession = person
 
# Unpacking a list
`numbers = [1, 2, 3]
one, two, three = numbers'`

**variable typs:**

 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 typing
my_variable = 10        # my_variable is an integer
my_variable = "Hello"   # my_variable is now a string`

You can check the type of a variable using the type() function.

``Example:
my_var = 42
print(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 constant
`PI = 3.14159
MAX_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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imntmpgpixe7pkppl5zs.png)

Float (float): Decimal numbers.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lrhz89uom30nvtd78nt5.png)

Complex (complex): Complex numbers.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3v82qhyfjndqa8i0sot5.png)

**2. Text Type**

 String (str): Sequence of characters.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gwgybgtfkpbqilfjzhk4.png)
**3. Boolean Type**

Boolean (bool): Represents True or False


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ggc27tj0ohtfs0e83eyw.png)

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



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7gfw84gr8m008qk4smdr.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qxfbbr62wpwntir8js1.png)
Tuple (tuple): Ordered, immutable collection.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wnukkofusffcnytemub3.png)
Range (range): Sequence of numbers.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vbbgvatom8hms6mcfgi.png)
Mapping Type
Dictionary (dict): Unordered, mutable collection of key-value pairs.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xb2vjnz5fhpbc4ptuh08.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bb1i448pyox0h78xuri9.png)
Frozenset (frozenset): Immutable set.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/simoibjmlfqv0zgk4ws6.png)

Checking Data Type
Syntax: type(variable_name)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mge8im093zf30g7nl194.png)

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.
Agenda:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vc10t8zk2avlchxfxsvf.jpg)

Tasks:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w652z0fnmsrn4r2bhl2i.jpg)

Playlist:
https://www.youtube.com/live/5G0PoJofxXk?si=Wo82t4JJYeP9WcR2