Introduction

Python is a popular high-level, interpreted, and general-purpose dynamic programming language that was created by Guido van Rossum and released in 1991.

It is used for web development, software development, mathematics, and system scripting. Python can be treated in a procedural way, an object-oriented way, or a functional way.

The most recent major version of Python is Python 3, which is used in most tutorials and courses. Python syntax is designed for readability and uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses. Python relies on indentation, using whitespace, to define scope, such as the scope of loops, functions, and classes.

Python is easy to learn and use, making it a popular choice for beginners and experienced programmers alike. There are many resources available for learning Python, including online tutorials, courses, and documentation.

What’s New In Python 3.12

Python 3.12 is the latest stable release of the Python programming language, with a mix of changes to the language and the standard library. The library changes focus on cleaning up deprecated APIs, usability, and correctness. Of note, the distutils package has been removed from the standard library. Filesystem support in os and pathlib has seen a number of improvements, and several modules have better performance.

The language changes focus on usability, as f-strings have had many limitations removed and ‘Did you mean …’ suggestions continue to improve. The new type parameter syntax and type statement improve ergonomics for using generic types and type aliases with static type checkers.

This article doesn’t attempt to provide a complete specification of all new features, but instead gives a convenient overview. For full details, you should refer to the documentation, such as the Library Reference and Language Reference. If you want to understand the complete implementation and design rationale for a change, refer to the PEP for a particular new feature; but note that PEPs usually are not kept up-to-date once a feature has been fully implemented.

Installation

Windows

  1. Visit the Windows downloads page in the Python website and download the installer based on your local machine's hardware architecture.
  2. Once downloaded, run the installer and follow the installation instructions
  3. When the installation finishes, run the code below in order to know your python version (omitting the "$" symbol) $ python --version
    Text copied to clipboard!

Mac OS X

  1. Visit the Mac OS X downloads page in the Python website and download the installer based on your local machine's hardware architecture.
  2. Once downloaded, run the installer and follow the installation instructions
  3. When the installation finishes, run the code below in order to know your python version (omitting the "$" symbol) $ python --version
    Text copied to clipboard!

GNU/Linux

  1. Most of the Linux Distribution have python preinstalled in their system. In order to know what version of it you have, run the code below (omitting the "$" symbol) $ which python3
    Text copied to clipboard!
  2. Anyway, you can install any version of python by running the following code (omitting the "$" symbol) $ sudo apt install python
    Text copied to clipboard!
    $ sudo apt install python3
    Text copied to clipboard!
    Important: Make sure you install the correct python version, python2 or python3 that are very diferents, bear in mind.
  3. When the installation finishes, run the code below in order to know your python version (omitting the "$" symbol) $ python --version
    Text copied to clipboard!
    $ python3 --version
    Text copied to clipboard!
Hello World

To get started with Python, let's write your first program. The "Hello World" program. So, first of all, open your interpreter with the line of code below

$ python
Text copied to clipboard!

Already with the interpreter active, run the following line of code

>>> print("Hello World")
Text copied to clipboard!

That's all, you have written your first program with python

Variables

Python variables are symbolic names that serve as references or pointers to objects in a program

They are containers for storing data values and can be used to store various types of data, such as integers, strings, and more complex objects

Python does not require variable declaration, and a variable is created the moment you first assign a value to it

Here are some key points about Python variables:

  • Creation: A variable is created when you first assign a value to it
  • Data-type: Python variables do not need to be declared with any particular type, and they can even change type after they have been set
  • Case-sensitive: Variable names are case-sensitive, meaning that name, Name, and NAME are considered different variables
  • Rules: Python variable names must start with a letter or underscore, cannot start with a number, and can only contain alpha-numeric characters and underscores
  • Scope: Variables in Python have a global scope by default, meaning they can be accessed from anywhere in the program. However, you can create local variables within functions to limit their scope

To create a variable in Python, simply assign a value to a name:

x = 5
y = "John"
Text copied to clipboard!

You can also change the value of a variable during program execution:

x = 5
x = 10
print(x) # Output: 10
Text copied to clipboard!

To get the data type of a variable, you can use the type() function:

x = 5
print(type(x)) # Output: class 'int'
Text copied to clipboard!
Constants

In Python, constants are typically represented using variables with names written in all capital letters as a naming convention, but Python does not have a dedicated constant type.

Constants are used to store values that are not meant to be changed during the execution of a program. While the language does not prevent reassignment of constants, the naming convention serves as a signal to other programmers that the variable should be treated as a constant.

Here are some key points about Python constants in relation to variables:

  • Definition: Python does not have a dedicated syntax for defining constants. Instead, constants are usually represented using variables with names in all capital letters as a naming convention
  • Usage: Constants are typically declared and assigned in a separate module or file, and then imported into the main file where they are used
  • Convention: The use of all capital letters for the names of constants is a convention to distinguish them from regular variables, but it does not actually prevent reassignment
  • Purpose: Constants are used to improve code readability, maintainability, and to signal the intended immutability of certain values

Here is an example of how to create a constant in Python:

# Declare constants in a separate file and name that file with the .py extension
# Constants.py file
SPEED_OF_LIGHT_IN_VACUUM = 299792458
PI = 3.141592653589793
LUMINOUS_EFFICACY = 683

# Example.py file
import Constants

print(Constants.SPEED_OF_LIGHT_IN_VACUUM)
print(Constants.PI)
print(Constants.LUMINOUS_EFFICACY)
Text copied to clipboard!

In the above example, we have created the Constants.py file which is called the Constants module. Then, we declared some constant values. After that, we create another Python file which is an Example.py file and in that file, we imported the Constants module using the import keyword. And finally, accessed the constant values. The intention of using capital letters is to tell other programmers that the variable should be treated as a constant

Data types

Python has several built-in data types, including numeric, sequence, boolean, set, dictionary, and binary types. Here is a brief overview of each:

  • Numeric Types: Python supports integers, floating-point numbers, and complex numbers. These are used to represent numeric data.
  • Sequence Types: Python has several sequence types, including lists, tuples, and ranges. These are used to store collections of items.
  • Boolean Type: The boolean type is used to represent truth values, True and False.
  • Set Types: Python includes set and frozenset types, which are used to store unique elements.
  • Dictionary Type: Dictionaries are used to store key-value pairs.
  • Binary Types: Python includes bytes, bytearray, and memoryview, which are used to store binary data.

These data types are used to define the type of a variable and the kind of value it can hold. Python variables are not declared with a specific data type and can change types easily during program execution

Conditionals

Conditional statements are an essential part of programming in Python. They allow you to make decisions based on the values of variables or the result of comparisons. The primary conditional statements in Python are the "if," "else," and "elif" statements.

Here are some key points about Python conditionals:

  • if Statement: The if statement is used to execute a block of code if a certain condition is true.
  • if-else Statement: The if-else statement is used to execute one block of code if a condition is true, and another block of code if the condition is false.
  • if-elif-else Statement: The if-elif-else statement is used to check multiple conditions in sequence, and execute different code blocks depending on which condition is true.
  • Conditional Expression: The conditional expression is a compact way to evaluate a condition and return a value based on the result.

These conditional statements allow for the execution of different blocks of code based on the value of variables. Remember to use proper indentation and syntax when writing conditional statements

Loops

A loop is a control structure that repeats a block of code as long as a certain condition is met.

There are two main types of loops in Python:

  • For Loop: The "for" loop is used to iterate over a sequence of elements, such as a list, tuple, dictionary, set, or string. It iterates over each item in the sequence and executes the block of code for each item. Here's an example: fruits = ["apple", "banana", "cherry"]
    for fruit in fruits:
    print(fruit)
    Text copied to clipboard!
  • While Loop: The "while" loop is used to execute a block of statements repeatedly until a given condition is no longer satisfied. It continues to execute as long as the condition remains true. Here's an example: count = 0
    while count < 5:
    print(count)
    count += 1
    Text copied to clipboard!

Both types of loops can be controlled using loop control statements such as "break" and "continue" to alter the flow of the loop based on certain conditions. Additionally, nested loops, where a loop is present inside another loop, can also be used for more complex iterations.

These loops are fundamental for repetitive execution of code and iteration over data structures in Python

Functions

In Python, a function is a block of code that performs a specific task. It is a reusable piece of code that can be called from other parts of a program. Functions can be used to divide a complex problem into smaller chunks, making the program easy to understand and reuse.

There are two types of functions in Python: standard library functions and user-defined functions. Standard library functions are built-in functions in Python that are available to use, while user-defined functions can be created based on specific requirements.

To define a function in Python, the def keyword is used, followed by the function name and any arguments that the function takes. The function body is then defined, and the function can optionally return a value. Functions can be called from other parts of the program by using the function name and passing any required arguments.

Python also has several built-in functions and types that are always available, such as abs(), len(), and print(). These built-in functions can be used to perform common tasks without having to write custom code.

Variables are also an essential part of programming in Python. They are like containers that hold values and can be used to store anything from simple data types like integers and strings to more complex objects. Variables are useful because they can help keep track of different pieces of information in a program.

Overall, functions and variables are fundamental concepts in Python programming that are used to create efficient and reusable code

References

Most of the information presented here is taken from the following sites: