Programming

Introduction to Python programming language

Introduction to Python programming language

Hello everyone: ), this article is about Python language which is an object-oriented and high-level programming language.

This article will be very beneficial for you to improve your knowledge of the basics of Python language. So, let’s start ;

Python

python programming language

Python is a popular, high-level programming language known for its simplicity, readability, and flexibility. It is widely used in a variety of fields, including web development, data science, scientific computing, and artificial intelligence. Python also supports object-oriented, procedural, and functional programming paradigms. This makes it a versatile language that can be used for a wide range of purposes.

Types of variables

  1. Numeric variables
  2. String variables
  3. Boolean variables
  4. List variables
  5. Dictionary variables
  6. Function variables

Numeric Variable

These are variables that store numeric values, such as integers (whole numbers) and floating-point numbers (numbers with decimal places).

Example:

x = 10;          # integer variable
y = 3.14;      # floating-point variable

String variable

These are variables that store sequences of characters, enclosed in quotation marks. They are commonly used to store text-based data.

Example:

name = "John Doe";    # string variable
greeting = 'Hello';       # string variable

Boolean variable

These are variables that can have one of two values: True or False. They are often used to store the result of a logical expression or to control the flow of a program.

Example:

is_raining = True;    # boolean variable
is_cold = False;        # boolean variable

List variable

These are variables that store an ordered collection of values. These are similar to arrays in other programming languages.

Example:

names = ["John", "Jane", "Joe"];          # list variable
numbers = [1, 2, 3, 4, 5];                        # list variable

Dictionary variable

These are variables that store unordered collections of key-value pairs. They are often used to store data that needs to be accessed by a unique identifier (the key).

Example:

person = {
    "name": "John Doe",
    "age": 35,
    "gender": "Male"
}                                                          # dictionary variable

Function variable

These are variables that refer to a function or a method. They are often used to store a reference to a function that can be called later.

Example:

def greet(name):
    print("Hello, " + name)

greet_func = greet;                               # function variable

Datatypes in Python

  1. Numeric type
  2. Sequence type
  3. Mapping type
  4. Set type
  5. Boolean type
  6. Binary type
  7. None type

Numeric Type

These are data types that represent numeric values, such as integers, floating-point numbers, and complex numbers.

Example:

x = 10;                    # integer
y = 3.14;                # floating-point
z = 2+3j;                # complex

Sequence Type

These are data types that represent ordered collections of values, such as strings, lists, and tuples.

Example:

name = "John Doe";                                 # string
numbers = [1, 2, 3];                                  # list
person = ("John", 35, "Male");               # tuple

Mapping Type

These are data types that represent unordered collections of key-value pairs, such as dictionaries.

Example:

person = {
    "name": "John Doe",
    "age": 35,
    "gender": "Male"
};                                                       # dictionary

Set Type

It represents unordered collections of unique values, such as sets.

Example:

numbers = {1, 2, 3, 3, 4, 5};            # set

Boolean Type

This is a data type that represents a logical value, either True or False.

Example:

is_raining = True;           # boolean
is_cold = False;                # boolean

Binary Type

It represents binary data, such as bytes and byte arrays.

Example:

binary_data = b"\x01\x02\x03";                                 # bytes
binary_array = bytearray(b"\x01\x02\x03");          # bytearray

None Type

This is a special data type that represents the absence of a value.

Example:

x = None;        # None type

Operators in Python

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Logical operators
  5. Bitwise operators
  6. Membership operators
  7. Identity operators

Arithmetic Operator

These are operators that perform basic mathematical operations, such as addition, subtraction, multiplication, and division.

Example:

x = 5 + 2;           # addition
y = 5 - 2;            # subtraction
z = 5 * 2;           # multiplication
a = 5 / 2;           # division

Assignment Operator

These are operators that are used to assign a value to a variable.

Example:

x = 5;        # assignment operator

Comparison Operator

Comparison operators compare two values and return a boolean value (True or False) based on the result of the comparison.

Example:

x = 5;
y = 2;

result = x > y;              # greater than operator
result = x >= y;           # greater than or equal to operator
result = x < y;             # less than operator
result = x <= y;          # less than or equal to operator
result = x == y;          # equal to operator
result = x != y;           # not equal to operator

Logical Operator

It performs logical operations, such as and, or, not, on boolean values.

Example:

x = True;
y = False;

result = x and y;            # logical and operator
result = x or y;              # logical or operator
result = not x;              # logical not operator

Bitwise Operator

It performs bitwise operations on the binary representation of numeric values.

Example:

x = 5;                         # binary representation: 0101
y = 2;                         # binary representation: 0010

result = x & y          # bitwise and operator
result = x | y           # bitwise or operator
result = x ^ y         # bitwise xor operator
result = ~x             # bitwise not operator
result = x << 1      # bitwise left shift operator
result = x >> 1      # bitwise right shift operator

Membership Operator

Membership operators check if a value exists in a sequence, such as a string, a list, or a tuple.

Example:

x = "hello";

result = "h" in x;                 #in operator
result = "z" not in x;         # not in operator

Identity Operator

Identity operators check if two values refer to the same object in memory.

Example:

x = "hello";
y = x;

result = x is y;                  # is operator
result = x is not y;           # is not operator

Functions of Python

  1. print(): – This function is used to display output on the screen.
  2. input(): – This function is used to get input from the user.
  3. len(): – This function is used to get the length of a string.
  4. range(): – This function is used to generate a sequence of numbers.
  5. sum(): – This function is used to add up the numbers in a list.
  6. min(): – This function is used to get the minimum value in a list.
  7. max(): – This function is used to get the maximum value in a list.

These are just a few examples of the many functions available in Python. There are many more, and you can find them in the Python documentation or by using the dir() function to list the available functions in a module or library.

If you have any queries regarding the above content, or you want to update anything in the content then contact us with your queries. You can directly post your question in the group.