Here, You can read basic concepts of machine learning and enhance your level manifolds.
Fundamentals of python

Keywords

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:

  1. and - It is a logical operator that returns True if both the operands are True.
  2. as - It is used to create an alias for a module or a function.
  3. assert - It is used to check if a condition is True. If the condition is False, an error is raised.
  4. break - It is used to exit from a loop prematurely.
  5. class - It is used to define a class in Python.
  6. continue - It is used to skip the current iteration of a loop and move to the next.
  7. def - It is used to define a function in Python.
  8. del - It is used to delete a reference to an object in Python.
  9. elif - It is short for "else if" and is used to check multiple conditions in an if-else.
  10. else - It is used in an if-else statement to specify the block of code that should be executed if the condition is False.

Don't worry about this terminology, we will learn how to use these keywords later on.


Identifiers

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:

  1. The first character of an identifier must be a letter or an underscore ( _ ).
  2. The rest of the characters can be letters, digits, or underscores.
  3. Identifiers are case-sensitive. For example, myVar and myvar are two different identifiers.
  4. Identifiers cannot be the same as Python keywords.
  5. Identifiers should be descriptive and meaningful to help other programmers understand the code.

Example

# 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.


Indentation

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.

Variables

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.


Multiple Assignment

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.

Example

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