DEV Community

Ranjith srt
Ranjith srt

Posted on • Edited on

3.PYTHON-FUNDAMENTALS: CONSTANTS VARIABLES AND DATA TYPES

PYTHON-FUNDAMENTALS:
constants variables and data Types

Variables:

Definition of Variables in Python

A variable is a named memory location in which a value is stored.
Any data type in Python, such as an integer, string, or list, can be used as the value.
Variables are used to store information that will be needed during the program.
In Python, you do not need to define a variable before using it.
When you assign a value to a variable, it is created.
You can change a variable's value at any moment. This will override its previous value.

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:

my_var = "Python"
print(type(my_var))
# Output: `

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

Float (float): Decimal numbers.

Image description

Complex (complex): Complex numbers.

Image description

2. Text Type

String (str): Sequence of characters.

Image description
3. Boolean Type

Boolean (bool): Represents True or False

Image description

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

Image description

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

Image description
Tuple (tuple): Ordered, immutable collection.

Image description
Range (range): Sequence of numbers.

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

Image description

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

Image description
Frozenset (frozenset): Immutable set.

Image description

Checking Data Type
Syntax: type(variable_name)

Image description

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

Tasks:

Image description

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

Top comments (0)