Python Variables
Python Variables
PYTHON VARIABLES
• A variable is a reserved memory area (memory address) to store value. For example, we want to store an employee’s
salary. In such a case, we can create a variable and store salary using it.
• In other words, a variable is a value that varies according to the condition or input pass to the program. Everything in
Python is treated as an object so every variable is nothing but an object in Python.
• A variable can be either mutable or immutable. If the variable’s value can change, the object is called mutable, while if
the value cannot change, the object is called immutable. article.
CREATING A VARIABLE
• Python programming language is dynamically typed, so there is no need to declare a variable before using it
• We can assign a value to the variable at that time variable is created. We can use the assignment operator = to assign a
value to a variable.
C H A N G I N G T H E VA L U E O F A VA R I A B L E
C R E A T E N U M B E R , S T R I N G , L I S T VA R I A B L E S
• Number A number is a data type to store numeric values. The object for the number will be created when we assign a
value to the variable. In Python3, we can use the following three data types to store numeric values.
1. Int
2. float
3. complex
• Integer variable
The int is a data type that returns integer type values (signed integers); they are also called ints or integers. The integer value
can be positive or negative without a decimal point.
CONT.
• Float variable
Floats are the values with the decimal point dividing the integer and the fractional parts of the number. Use float data
type to store decimal values.
Complex type
The complex is the numbers that come with the real and imaginary part. A complex number is in the form of a+bj, where a
and b contain integers or floating-point values.
CONT.
String variable
• In Python, a string is a set of characters represented in quotation marks. Python allows us to define a string in
either pair of single or double quotation marks. For example, to store a person’s name we can use a string type.
• To retrieve a piece of string from a given string, we can use to slice operator [] or [:]. Slicing provides us the subset of a
string with an index starting from index 0 to index end-1.
• To concatenate the string, we can use the addition(+) operator.
CONT.
• No matter what is stored in a variable (object), a variable can be any type like int, float, str, list, tuple, dict, etc. There is
a built-in function called type() to get the data type of any variable.
• Syntax of type() :
DELETE A VARIABLE
• Use the del keyword to delete the variable. Once we delete the variable, it will not be longer accessible and eligible for
the garbage collector.
RULES AND NAMING CONVENTION FOR VARIABLES AND CONSTANTS
A name in a Python program is called an identifier. An identifier can be a variable name, class name, function name, and
module name.
• Rule 1: The name of the variable and constant should have a combination of letters, digits, and underscore symbols.
• Rule 2: The variable name and constant name should make sense.
• Rule 3: Don’t’ use special symbols in a variable name
• Rule 4: Variable and constant should not start with digit letters.
• Rule 5: Identifiers are case sensitive.
• Rule 6: To declare constant should use capital letters.
• Rule 6: Use an underscore symbol for separating the words in a variable name
MULTIPLE ASSIGNMENTS
In Python, there is no restriction to declare a variable before using it in the program. Python allows us to create a variable
as and when required.
We can do multiple assignments in two ways, either by assigning
• a single value to multiple variables
• assigning multiple values to multiple variables.
Operators are special symbols that perform specific operations on one or more operands (values) and then return a result. For example, you can calculate
the sum of two numbers using an addition (+) operator.
Python has seven types of operators that we can use to perform different operation and produce a result.
1. Arithmetic operator
2. Relational operators
3. Assignment operators
4. Logical operators
5. Membership operators
6. Identity operators
7. Bitwise operators
ARITHMETIC OPERATOR
• There are seven arithmetic operators we can use to perform different mathematical operations, such as:
• + (Addition)
• - (Subtraction)
• * (Multiplication)
• / (Division)
• // Floor division)
• ℅ (Modulus)
• ** (Exponentiation)
CONT.
Logical Operator
Logical operators are useful when checking a condition is true or not. Python has three logical operators. All logical
operator returns a boolean value True or False depending on the condition in which it is used.
CONT.
MEMBERSHIP OPERATORS
• Python’s membership operators are used to check for membership of objects in sequence, such as string, list, tuple. It
checks whether the given value or variable is present in a given sequence. If present, it will return True else False.
• In Python, there are two membership operator in and not in
• In operator
• It returns a result as True if it finds a given object in the sequence. Otherwise, it returns False.
CONT.
• Not in operator
It returns True if the object is not present in a given sequence. Otherwise, it returns False
BITWISE OPERATORS
• In Python, bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into
binary and then operations are performed on each bit or corresponding pair of bits, hence the name bitwise operators.
The result is then returned in decimal format.
CONT.
CONT.
PYTHON IDENTITY OPERATORS
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the
same memory location:
PYTHON LOGICAL OPERATORS