In Python, keywords are reserved words that have specific meanings and functions within the language. These keywords cannot be used as variable names or any other identifiers. Some of the keywords used in python are:
Don't worry about this terminology, we will learn how to use these keywords later on.
In Python, an identifier is a name given to a variable, function, class, or any other user-defined object. An identifier is used to refer to an object in the code. Identifiers in Python follow certain rules:
# Variables
my_variable = 5
user_input = input("Enter your name: ")
# Functions
def my_function():
print("Hello World")
# Classes
class MyClass:
def __init__(self, name):
self.name = name
# Object instances
obj = MyClass("Yuvi")
Note: In python comments that is non-executable statements always starts with '#' symbol. So, in the above example entire green coloured text are comments.
In Python, indentation is used to indicate the level of nesting in the code. Unlike other programming languages where braces ({}) are used to denote blocks of code, Python uses indentation to separate blocks of code. The amount of indentation in each line must be consistent, and it is usually four spaces per level of indentation.
For example , in a control statement such as an if-else statement, the code that belongs to the if statement is indented, and the code that belongs to the else statement is also indented. Here is an example:
x = 5
if x > 0:
print("x is positive")
else:
print("x is non-positive")
In the above example, the code that belongs to the if statement (i.e., print("x is positive")) and the code that belongs to the else statement (i.e., print("x is non-positive")) are indented.
It is important to note that the level of indentation in Python is significant and must be consistent throughout the code. Mixing tabs and spaces for indentation can result in syntax errors. Therefore, it is recommended to use spaces for indentation instead of tabs.
Indentation is one of the distinctive features of Python, and it enforces code readability and consistency. It is also a visual representation of the code structure, making it easier to understand the program flow.
In Python, a variable is a named storage location that holds a value. The value can be of any data type, such as numbers, strings, lists, tuples, dictionaries, etc. A variable is created when a value is assigned to it using the assignment operator (=).
Here is an example of how to create a variable and assign a value to it:
x = 5
In the above example, x is a variable that holds the value 5. The value of x can be accessed or modified by using the variable name in the code.
Variables can be used in expressions and statements to perform operations. For example, we can use variables to perform arithmetic operations:
x = 5
y = 10
z = x + y
print(z)
#Output: 15
In the above example, x and y are variables that hold the values 5 and 10, respectively. We can perform the addition operation using these variables and store the result in the variable z. Finally, we print the value of z.
In Python, multiple assignment allows you to assign multiple values to multiple variables in a single statement. This is a convenient feature in Python, especially when dealing with large amounts of data, as it reduces the amount of code needed to perform the same task.
Here is an example of multiple assignment in Python:
x, y, z = 5, 10, 15
In the above example, we are assigning the values 5, 10, and 15 to the variables x, y, and z, respectively, in a single line. The order of the variables and values must match, i.e., the first value is assigned to the first variable, the second value is assigned to the second variable, and so on.
We can also use multiple assignment to swap the values of two variables, as shown in the following example:
x, y = 5, 10
x, y = y, x
print(x) # Output: 10
print(y) # Output: 5
In the above example, we are swapping the values of the variables x and y using multiple assignment. The values of x and y are first assigned to y and x, respectively, and then printed to verify the swap.
Multiple assignment can also be used with functions that return multiple values, as shown in the following example:
def my_function():
return 5, 10
x, y = my_function()
print(x) # Output: 5
print(y) # Output: 10
In the above example, the function my_function() returns two values, 5 and 10. We can use multiple assignment to assign these values to the variables x and y, respectively.
Multiple assignment is a powerful feature in Python, and it can help you write more efficient and concise code. It is a useful tool for dealing with large amounts of data and simplifying complex operations.
Advertisement
Advertisement