So lets deep right into it!
What is Python?
Python is a general-purpose interpreted high-level programming language that came up in the 1980s by Guido van Rossum to develop computer applications, Machine learning, and Artificial Intelligence. Python’s design philosophy emphasizes code readability with its notable use of significant indentation.
Python is a programming language that is easy to learn and understand used to develop computer applications, ML, AI, etc.
Advantages of Python
It’s completely free and can be used with no copyright issues it can also be distributed although it’s free; Python has had a very active community for more than 25 years and is constantly being improved to keep it up to date keep up with the times
It is a multi-paradigm
Python is a multi-paradigm language that supports both procedural programming (using functions) and object-oriented programming (including functions such as single and multiple inheritances, operator overloading, and duck typing) elements of functional programming (as integrators and generators).
It is portable
Python developed by ANSI C as a portable language. It can be used on various platforms such as UNIX, Linux, Windows, DOS, Macintosh, Real-Time Systems, OS / 2, Android, and ios mobile phones. This is possible because it is an interpreted language, so the same code can be executed on any platform as long as the Python interpreter is installed.
Easy to Use
Python is a simple but powerful language. The syntax and the various modules and functions already included in the language are consistent, intuitive, and easy to learn, and the language design is based on the principle of least surprise (the program is what is expected).
It’s full of libraries
Every Python installation includes the standard library, which is a collection of more than 200 modules used to perform a wide variety of tasks, such as: For interaction with the operating system and the file. System or the management of various protocols. Package Index allows you to download and install thousands of additional modules that are created and maintained by the community.
Although Python is considered an interpreted language, programs are automatically compiled into a format called bytecode before they are executed. This format is clean and efficient and therefore ensures high performance. In addition, various data structures, functions, and modules are made within C to achieve the highest possible performance.
Automatic memory management
As mentioned earlier, Python is a simple high-level programming language. Uses a garbage collection mechanism to allocate and free memory so that the programmer can freely use variables without having to worry about their manual declaration and assignment and freeing up memory locations (which is required in lower languages like C or C ++).
Can be integrated with other languages
In addition to the classic interpreter written in C (and called Python), there are also other interpreters that enable integration with various other languages. Iron Python enables you to use Python within the .NET framework, use its features, and interact with others.
To integrate Python and Java, you can use Python. There are other interpreters such as PyPy, a high-performance implementation written in Python.
Basis of Python Explained
1. Python is interpreted and high-level language
Time to start our exploration! Python is first of all an interpreted language.
Interpreted means that the Python code you write is read on the fly by a “translator” who transforms it into “machine” language, which is a language that can only be understood by your computer’s processor.
From now on I will refer to the “translator” as the Python engine, the tool that reads and interprets our code.
The transition from code to execution is therefore almost immediate and you don’t have to worry about low-level details like memory or processor instructions.
In addition to the interpreted programming languages there are also compiled ones, where the code you write speaks more directly to the machine, and must be translated by a tool called a compiler (examples of compiled languages are C, C #, Java).
The compiler then produces your little program, called binary, which you can run on your computer. The transition from code to execution here is slightly more cumbersome. Compiled languages are even more difficult to master than interpreted languages because they speak to the machine closely.
Interpreted languages such as Python, on the other hand, can be written in a “language” very similar to English, and for this reason Python is said to be high level.
2. Python is dynamic programming language
In addition to being interpreted and high-level, Python is also a dynamic language.
In dynamic programming this means that if we have a variable (think of the variable as a box) that contains a cat, later in the code we can mistake the cat for a human.
Consider this example in pseudo code (it’s not Python):
""" Language with dynamic types """ container = cat container = human # all right!
Container is first a cat, and then becomes a human, and the program is flawless. Types are said to be dynamic.
The opposite of a dynamic language is static language, where if a variable (the container) contains a type of cat, later it is not possible to remove the cat and put a human in it:
""" Language with static types """ container: cat = siamese container = human # error! The program stops
The difference between static and dynamic languages is that the former are more rigid than the latter, and in some ways more secure, especially for “critical” applications such as financial ones (examples of static languages, that is statically typed, are C #, Java, TypeScript).
For some years also Python can be transformed into a static language with the so-called type annotations (advanced concept, but if you have the time and desire, watch this video of mine).
3. Python is object oriented programming language
In any tutorial, Python is referred to as an object-oriented language.
Ok but what are these objects? What a programmer does is simply take concepts from the real world like ship, house, person, car, and tree and transport them into code.
When we take an entity as a “tree” and pin it in Python code as a Tree, we are just creating a new object.
Python is said to be object oriented because it is very easy to create new objects and think about them in a very comfortable way.
Later you will understand better what it is.
Python for Beginners: Python is style conscious
One of the most important features of Python is the focus on style.
By style I don’t just mean good or bad code, but also how it is interpreted by the Python engine (remember the Python engine is the tool that reads and interprets the code).
Let’s take a practical example, with your first line of Python code. Look at this instruction:
print (“Welcome to Python World with 360TechExplorer!”)
You can see how it is shifted four spaces to the right (one tab stop). If I run this Python code I will get an error (also called an exception): Indentation Error.
What does it mean? In Python it is important not only to write “beautiful” code but also to respect the so-called indentation, or the tabulation of the code.
The Python engine in fact reads the code precisely based on how it is indented. The example above will not run, but respecting the tabulation, like this:
You will see “Hello Python!” printed somewhere (you will soon find out where).
4. How to setup Python?
Disclaimer: Before continuing read here if you don’t know what a terminal is.
To use Python you need to download and install it on your computer.
Python is already installed on some operating systems such as Linux (Fedora, Ubuntu), while for MacOs and Windows you have to download and install it by hand. I refer you here for the download, in this section I want to quickly explain how to run Python code.
After installing Python (make sure you install version 3.x) you should have the python3 executable on your system.
There are two ways to use Python on your computer. The first option is to create files with Python code and .py extension, and then launch them with the Python executable.
Example, to print a simple message on the terminal creates a file named hello.py with the following code:
print (“Hello Python!”)
And then launch it with:
$ python3 hello.py
When creating Python files I suggest you name the file with a descriptive name, which does not contain spaces. Example hello.py is fine, but “my python file is nice.py” is not. Print (“Hello Python!”) Is not okay either.
Second option for running Python code: the Python console. After installing Python, open a terminal and run the python3 executable:
You will find yourself in the Python console (you should see the “>>>” symbol) where you can directly launch small instructions and write code for quick tests:
>>> print (“Hello Python!”)
In the examples below you will find both Python files and instructions launched directly into the console.
5. Python Data Types
Let’s finally begin our exploration of Python, starting with “data types”.
When we talk about types of data in programming we are referring to the fundamental blocks on which everything else is built.
But to be more precise, “the type” is a bit like what the species is to life on earth. We, for example, are Homo sapiens, while wolves are of the Canis lupus species.
In the same way, there can be data of various kinds in programming.
For example, in Python a data can be:
|str||“Any string within quote”||string|
Float is a decimal number, while int is an integer (integer is a Latin word ;-)).
The string is the text string; Boolean is a Boolean value that can be true or false. None instead is a null value, a sort of placeholder that has a very specific use and that you will see later.
Each type of data corresponds to a value that can be associated with a container, the so-called variables.
6. Variables in Python programming
Note: From this moment to practice you can launch all the examples I propose to you in the Python console (or save them in a file and launch them with the executable).
In “Python is dynamic” I talked about variables as boxes to put “stuff” in. The variables in Python are everywhere and are used to hold data.
When we create a new variable we are declaring it and to declare variables in Python just fix its name, followed by a value. Here are some examples:
count = 105.00
name = "Rohit" age = 17 city = "Chicago" married = False count = 91.00
When a variable is assigned a particular type, that variable becomes “of that type”. Above for example “name” is a string; “age” is a number, and so on.
Always reconnecting to “Python is dynamic”, even if a variable is born a string it does not necessarily have to die:
name = "Rohit" name = 18 # First name is a string # and then becomes a number
With good reason, even if Python allows it you don’t necessarily have to swap types all the time. A variable called “name” has no reason to become a number.
In static type languages like Java or Typescript this is not allowed so freely, and even Python has an optional “safe” as I mentioned earlier.
It is also worth clarifying that Python treats as variables all names that are not contained in quotes in the program . This means the following example:
name = “Liz”
Read as a Liz string assigned to the variable name. This code, on the other hand, means something else and is not even valid:
“Name” = “Liz”
When we talk about functions you will see why it is important to pay attention to these details.
7. Writing variables in the Python way
Not only does Python expect to find well-aligned code, you also have to pay attention to some rules that the Python world has set itself to make the work of developers more enjoyable.
Let’s take some examples. In some programming languages the variables are written with the so-called camel case form:
# don’t do this in python myVariable = “Liz”
In Python this form is to be avoided because it is neither common nor accepted by the community. The correct form instead is:
# ok! my_variable = “Liz”
Then separate the long names with an underscore. Another thing to avoid is capitalization (apart from a particular case):
# Don’t do this in Python MYVARIABLE = “Liz”
Another common but understandable mistake that many beginners make is not having consistency in writing variable names.
It is not uncommon to see such things (also encouraged by official teaching material for preparation for some exams):
Name = "Rohit" AGE = 17 CITY = "Chicago" Married = False
As much as I love the Italian that I always defend against the madness of the breakeven point that if we do not have a business plan we cannot talk to the investors and then the competitor overtakes us, it is good practice in Python to write the variables in lowercase in English.
These little tweaks improve the readability of your code and make what you write familiar to other Python developers as well.
Also read: What is anaconda? Anaconda vs Python