Hello Everyone,
Welcome to Day 26 of DevOps Learning, Today we explore Python Data Types, variables, and Operators
Python stands out from other programming languages because of its building blocks data types, variables, and operators.
These foundational concepts are what allow programmers to unleash the full potential of Python’s capabilities and create truly powerful software.
In this article, we’ll dive into the world of Python’s building blocks, exploring how they work and providing practical examples to help you master these concepts.
Data Types in Python
A data type is a classification of data based on its characteristics. Python has several built-in data types, including:
- Numbers: Python supports three types of numbers: integers, floats, and complex numbers. Integers are whole numbers, while floats are decimal numbers. Complex numbers consist of real and imaginary parts.
x = 5 # integer
y = 3.14 # float
z = 2 + 3j # complex number
2. Strings: Strings are sequences of characters that are enclosed in quotes. Python supports single, double, and triple quotes for defining strings.
name = 'Gokul' # single quotes
age = "23" # double quotes
description = """This is a multiline string""" # triple quotes
3. Booleans: Boolean values are either True or False. They are often used in logical expressions and comparisons.
x = True
y = False
z = x and y # False
4. Lists: Python has several types of collections, starting with lists. Lists are ordered collections of elements. They can contain elements of different data types and are mutable, meaning that they can be changed.
my_list = [1, 2, 'three', True]
my_list[2] = 'four'
print(my_list) # [1, 2, 'four', True]
5. Tuples: Tuples are similar to lists, but they are immutable, meaning that they cannot be changed. It is an ordered and unchangeable collection of objects.
my_tuple = (1, 2, 'three', True)
# my_tuple[2] = 'four' # TypeError: 'tuple' object does not support item assignment
6. Sets: Sets are unordered collections of unique elements. They are mutable and can be modified using methods such as add() and remove(). It is used to store an unordered and unindexed collection of unique objects.
my_set = {1, 2, 'three', True}
my_set.add('four')
print(my_set) # {1, 2, 'three', True, 'four'}
- Dictionaries: Dictionaries are collections of key-value pairs. They are unordered and can be modified using methods such as update() and pop(). It is used to store an unordered key and its value pairs.
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
my_dict['age'] = 30
print(my_dict) # {'name': 'John', 'age': 30, 'city': 'New York'}
Variables in Python
Variables are used to store values in Python. They are essentially labels that are assigned to a value. In Python, variables are created by assigning a value to a name using the equals (=) operator. It is only a name given to a memory location, all the operations done on the variable effects that memory location.
Rules for creating variable names in Python :
In Python, variables are used to store data and are identified by their names. The naming of variables is essential as it makes the code more readable and helps the programmer understand what a particular variable represents. Here are some rules for creating variable names in Python:
Must start with a letter or underscore (_). It is recommended to use a letter as the first character in a variable name.
Can only contain letters, numbers, and underscores. They cannot contain spaces or special characters.
Variable names are case-sensitive. This means that variables named ‘myVar’ and ‘myvar’ are considered to be different variables.
Variable names should be descriptive and meaningful. They should reflect the purpose of the variable in the program.
Avoid using Python-reserved keywords as variable names. These words have special meanings in Python and cannot be used as variable names. Examples of reserved words include ‘if’, ‘else’, ‘for’, ‘while’, ‘and’, ‘or’, ‘not’, ‘True’, and ‘False’.
Variable names should be concise and easy to type. Long and complex variable names can make the code harder to read and can increase the likelihood of typos.
Here are some examples of valid variable names in Python:
age = 23
name = 'Gokul'
my_list = [1, 2, 3]
_total = 100
And here are some examples of invalid variable names:
2name = 'Gokul' # variable name cannot start with a number
my-var = 10 # variable name cannot contain special characters
for = 5 # 'for' is a reserved word and cannot be used as a variable name
MyVariableNameIsVeryLong = 20 # variable name should be concise and easy to type
Variables can be used in expressions and statements to represent the value that they are assigned to or can be reassigned to a different value.
x = 10
y = x + 2
Operators in Python
Operators are symbols or words that are used to perform operations on values in Python. Python supports a variety of operators, including:
Arithmetic Operators: These operators are used to perform mathematical operations, such as addition (+), subtraction (-), multiplication (*), division (/), and modulo (%).
x = 10
y = 3
print(x + y) # 13
print(x - y) # 7
print(x * y) # 30
print(x / y) # 3.3333...
print(x % y) # 1
Comparison Operators: These operators are used to compare two values and return a Boolean value of True or False. Examples of comparison operators include equal to (==), not equal to (!=), greater than (>), greater than or equal to (>=), less than (<), and less than or equal to (<=).
x = 10
y = 5
print(x == y) # False
print(x != y) # True
print(x > y) # True
print(x < y) # False
print(x >= y) # True
print(x <= y) # False
Logical Operators: These operators are used to combine Boolean values and return a Boolean result. Examples of logical operators include and, or, and not.
x = 5
y = 10
print(x < 7 and y > 5) # True
print(x < 3 or y > 15) # False
print(not x == y) # True
Assignment Operators: These operators are used to assign a value to a variable. Examples of assignment operators include =, +=, -=, *=, and /=.
x = 10
x += 5
print(x) # 15
x -= 3
print(x) # 12
x *= 2
print(x) # 24
x /= 4
print(x) # 6.0
Bitwise Operators: These operators are used to perform bitwise operations on binary numbers. Examples of bitwise operators include &, |, ^, <<, and >>.
x = 0b1010 # Binary representation of 10
y = 0b1100 # Binary representation of 12
print(x & y) # 0b1000, which is 8 in decimal
print(x | y) # 0b1110, which is 14 in decimal
print(x ^ y) # 0b0110, which is 6 in decimal
print(~x) # -11, since the bitwise complement of 0b1010 is -0b1011 in two's complement notation
print(x << 2) # 0b101000, which is 40 in decimal
print(y >> 2) # 0b0011, which is 3 in decimal
Membership Operators: These operators are used to test if a value is a member of a sequence. Examples of membership operators include in and not in.
x = [1, 2, 3]
print(2 in x) # True
print(4 not in x) # True
y = "hello world"
print("h" in y) # True
print("z" not in y) # True
Identity Operators: These operators are used to compare the memory location of two objects. They return True if the memory location of the two objects is the same and False otherwise. Python has two identity operators: is and is not.
# is operator
a = [1, 2, 3]
b = a
print(a is b) # True, since a and b refer to the same object in memory
c = [1, 2, 3]
print(a is c) # False, since a and c refer to different objects in memory
# is not operator
x = 5
y = 10
print(x is not y) # True, since x and y are different objects in memory
z = 5
print(x is not z) # False, since x and z refer to the same object in memory
In conclusion, understanding data types, variables, and operators is fundamental to programming in Python.
The data types available in Python include integers, floats, complex numbers, strings, booleans, lists, tuples, sets, and dictionaries. Variables are used to store data and can be named according to certain rules. Operators are used to perform operations on values and can be categorized into arithmetic, comparison, logical, assignment, bitwise, membership, and identity operators.
By utilizing these concepts and tools, Python programmers can perform a wide variety of tasks, from basic arithmetic operations to complex data analysis and machine learning tasks. Knowing the rules for creating variable names and understanding the syntax for using different types of operators can help programmers write clear, concise, and efficient code.
In summary, mastering data types, variables, and operators is an essential step for anyone learning to program in Python and is key to becoming proficient in the language.
Congrats🎉, On Learning the Python data types, variables, and operators.
Happy Learning :)