Variables in Python
In this article, I am going to discuss Variables in Python with Examples. All the data which we create in the program will be saved in some memory location on the system. The data can beanything, an integer, a complex number, a set of mixed values, etc.
What is a variable in Python?
Variable is the name that we give to the memory location which holds some data. With a
variable, we can store the data, access the data, and also manipulate the data. We can also
refer to variables as containers which store some information/data.
Python variables key Points:
- Python has no command for declaring a variable.
- A variable is created the moment you first assign a value to it.
- Variables do not need to be declared with any particular type and can even change type after they have been set.
- If you want to specify the data type of a variable, this can be done with casting.
Creating a variable in Python:
Python is a dynamically typed programming language. There is no need of specifying the data
type for a variable. See example below
x = 4 # x is of type int
y = "Code Config" # y is of type string
print(x)
print(y)
Python variable casting:
If you want to specify the data type of a variable, this can be done with casting.
x = str(2) # x will be '2'
y = int(2) # y will be 2
z = float(2) # z will be 2.0
Get the Type of Python Variable:
You can get the data type of a variable with type() unction.
x = 5
y = "Code-Config"
print(type(x))
print(type(y))
Single or Double Quotes?
String variables can be declared either by using single or double quotes, both are same:
x = "CodeConfig"
# is the same as
x = 'CodeConfig'
Case-Sensitive:
Python variable names are case-sensitive.
a = 4
A = "Nitika"
#A will not overwrite a because it is case sensitive
Can not use reserve Keyword in variables:
In Python creating a variable with keyword name is not allowed. like with using if, else or
for keyword
if = 10 # 'if' is reserved keyword & cannot be used as variable.
print(if)
Assigning multiple variables in single line:
In Python it is also possible to assigning multiple variables in single line, Check example
a,b,c = 1,2,3
print(a,b,c)
#Output: 1 2 3
Re-initialize the variables in Python:
In Python we can reinitialize the variable values. The old values will be overridden or
replaced with the new values.
a = 10
print("At first I have assigned value: ", a)
a = 20
print("On re-initialization of variable, now its value is: ", a)
#Please note value 10 will get overridden with 20
Operators in Python
In this article, I am going to discuss Operators in Python with Examples. In programming languages, an operator is a symbol that is applied to some operands i.e. variables, to perform certain operations on it.
Types Of Operators in Python:
Following is the summary of Python operators:
- Arithmetic Operators: The operators which are used to perform arithmetic operations like addition, subtraction, division etc. They are: +, -, *, /, %, **, //
- Relational Operators: The operators which are used to check for some relation like greater than or less than etc. between operands are called relational operators. They are: <, >, <=, >=, ==, !=
- Logical Operators: The operators which do some logical operation on the operands and return True or False are called logical operators. The logical operations can be ‘AND’, ‘OR’ etc.
- Assignment Operators: The operators which are used for assigning values to variables. ‘=’ is the assignment operator.
- Unary Minus Operator: The operator ‘-’ is called the Unary minus operator. It is used to negate the number.
- Membership Operators: The operators that are used to check whether a particular variable is part of another variable or not. They are ‘in’ and ‘not in’
- Identity Operators: The operators which are used to check for identity. They are ‘is’ and ‘is not’
Examples of Python Operators:
Arithmetic operators are used with numeric values to perform common mathematical operations:
x + y # Addition
x - y # Subtraction
x * y # Multiplication
x / y # Division
x % y # Modulus
x ** y # Exponentiation
x // y # Floor division
x == y # Equal
x != y # Not equal
x > y # Greater than
x < y # Less than
x >= y # Greater than or equal to
x <= y # Less than or equal to
x < 5 and x < 10 # Returns True if both statements are true
x < 5 or x < 4 # Returns True if one of the statements is true
not(x < 5 and x < 10) # Reverse the result, returns False if the result is true
x is y # Returns True if both variables are the same object
x is not y # Returns True if both variables are not the same object
x in y # Returns True if a sequence with the specified value is present in the object
not in y #Returns True if a sequence with the specified value is not present in the object
x & y #Sets each bit to 1 if both bits are 1
x | y #Sets each bit to 1 if one of two bits is 1
x ^ y #Sets each bit to 1 if only one of two bits is 1
~x #Inverts all the bits
x << 2 #Shift left by pushing zeros in from the right and let the leftmost bits fall off.
x >> 2 #Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off.
Operator Precedence
Operator precedence describes the order in which operations are performed. Parentheses have the highest precedence, meaning that expressions inside parentheses must be evaluated first and then outer.
print((2 + 3) - (2 + 3))
Data Types in Python
In programming, data type is an important concept. In this article, I am going to discussData Types in Python with Examples. The data can be categorized into different types like
integers, float numbers, boolean, string, etc. Categorizing the data is important in order to
perform the operations on it. Python variables can store data of different types, and different
types can do different things.
Python Built-in Data Types:
Python has the following built-in data types
Text Type: str Numeric Types: int,float,complexSequence Types: list,tuple,rangeMapping Type: dict Set Types: set,frozensetBoolean Type: bool Binary Types: bytes,bytearray,memoryviewNone Type NoneType Example of data type:
x = 20 # int x = 20.5 # float x = 1j # Complex x = ["one", "two", "three"] #list x = range(6) #range x = ("one", "two", "three") #tuple x = {"name" : "John", "age" : 36} #dict x = True #bool x = b"Hello" #bytes x = bytearray(5) #bytearray x = None #NoneType
No comments:
Post a Comment