Learn Python: From Zero to Hero

Muhammad Arshad
BITLogix
Published in
6 min readSep 15, 2022

Initially in my new series.

ASLAMUALIKUM / HELLO everyone,

I was considering creating a Python series to aid developers who want to understand the language’s fundamental concepts. Please stay tuned after this first chapter.

So, let’s begin by discussing what Python is. Python is a, in accordance with its inventor Guido van Rossum:

“high-level programming language, and its core design philosophy is all about code readability and a syntax which allows programmers to express concepts in a few lines of code.”

Python is a gorgeous programming language, so that was the initial incentive to learn it. Coding in it and expressing my ideas felt quite natural.

The versatility of Python code, which excels in data research, web development, and machine learning, was another factor. Python is the language of choice for backend web development at Quora, Pinterest, and Spotify. So, let’s find out a little bit about it.

The Fundamentals

1. Variables

In Python, a variable is a symbolic name that serves as a pointer or reference to an object. You can use the variable name to refer to an object once it has been assigned to it. However, the object itself still holds the data.

Variables can be compared to words that contain values. Just like that.

It is very simple to define a variable in Python and assign a value to it. Consider storing the value 32 in the variable “age.” Let’s proceed:

age = 32

How easy was that to do? You simply gave the variable “age” the value 32.

id = 302any_number = 100200

Additionally, you are free to give whatever other value you like to any other variables. The variable “id” stores the number 302 as you can see in the code above, and “any number” stores the value 10,0200.

Additionally, you are free to give whatever other value you like to any other variables. The variable “id” stores the number 302 as you can see in the code above, and “any number” stores the value 10,0200.

In addition to integers, we may also utilise floats, strings, Booleans (True/False), and many other data kinds.

# Booleanstrue_boolean = Truefalse_Boolean = False# Stringname = "Muhammad Arshad"# Floatpen_price = 5.80

2. Control Flow: conditional statements

Conditions, loops, and function calls all play a role in how a Python programme is controlled. Python offers three different kinds of control structures: Sequential is the standard mode.

To determine if a statement is True or False, “If” applies an expression. If True, the “if” statement’s instructions are carried out. For instance:

if True:print("Hello World!")if 5 > 4:print("5 is greater than 4")

Since 5 is greater than 4, the code for “print” is carried out.

If the “if” expression returns a false value, the “otherwise” statement will be carried out.

if 5 > 6:print("6 is greater than 5")else:print("6 is not greater than 5")

Because 5 is less than 6, the code in the “otherwise” expression will be carried out.

Additionally, you can use a “elif” statement:

if 5 > 6:print("5 is greater than 6")elif 6 > 5:print("5 is not greater than 6")else:print("5 is equal to 6")

3. Looping / Iterator

Pythons for loop is used to repeatedly loop through a list, tuple, string, or other iterable object. The process of traversing a sequence entails iteration.

We can iterate in various ways in Python. While and for are the two I’ll discuss.

While Looping: The code contained within the block will be executed while the statement is True. The number range printed by this code is therefore from 1 to 50.

num = 1while num <= 50:print(num)num += 1

The while loop requires a “loop condition,” and if it remains True, iteration continues. In this case, the loop condition is False when num is 11.

To help you grasp it better, here’s some more basic code:

loop_condition = Truewhile loop_condition:print("Loop Condition keeps: %s" %(loop_condition))loop_condition = False

Until we change the loop condition from True to False, the loop continues to iterate.

Applying the variable “num” to the block causes the “for” expression to iterate the block for you. This code will print from 1 to 50, much like the while code.

for i in range(1, 51):print(i)

See? It is really easy. The range spans from element 1 through element 51.

List: Collection | Array | Data Structure

The two most significant data structures in Python are arrays and lists. In Python, data are stored in lists, arrays, and lists. We can iterate, slice, and index using these data structures. However, there are little differences between them.

Consider storing the number 1 in a variable. But perhaps you’d like to store 2 now. And 3, 4, 5 …

Exists a different way for me to store all the integers I need without creating millions of variables? As you might have suspected, there is yet another way to keep them.

Using the list collection, you can keep a list of values (like these integers that you want). So let’s employ it:

constructing a list

Use square brackets to denote a list and add elements as necessary. The output is an empty list if no elements are passed inside the square brackets.

my_list = [] #create empty listprint(my_list)my_list = [1, 2, 3, ‘example’, 3.132] #creating list with dataprint(my_list)

Embedding Elements

Using the append(), extend(), and insert() functions, you can add the list’s elements.

Each element given to the append() function is added as a single element.

The extend() function gradually adds each element to the list.

The insert() function expands the list’s size and adds the given entry to the index value.

my_list = [1, 2, 3]print(my_list)my_list.append([555, 12]) #add as a single elementprint(my_list)my_list.extend([234, ‘more_example’]) #add as different elementsprint(my_list)my_list.insert(1, ‘insert_example’) #add element iprint(my_list)

Appending Elements

The “+” operator, which will accept another tuple to be appended to it, is used to append the values.

my_tuple = (1, 2, 3)my_tuple = my_tuple + (4, 5, 6) #add elementsprint(my_tuple)

Classes & Objects

Just a little theory

Objects are representations of things found in the real world, such as bicycles, dogs, and cars. Data and behaviour are the two main traits that the objects have in common.

Vehicle information includes things like the number of wheels, doors, and seats. Additionally, they display behaviour; they may accelerate, brake, indicate how much fuel is still in the tank, among many other things.

We define data in object-oriented programming as attributes and behaviour as methods. Again:

Data, Behaviour, and Attributes, and Methods

Defining a Class

In Python, class definitions begin with the class keyword, just as function definitions do.

The first string within the class is known as the docstring, and it contains a brief description of the class. Although not required, it is strongly advised.

Here’s a basic class definition.

class MyClass:‘This is a docstring. I have created a class’pass

A class defines all of its characteristics in a new local namespace. Attributes can be either data or functions.

It also contains unique properties that begin with double underscores. For example, __doc__ returns the class’s docstring.

When we define a class, a new class object with the same name is produced. This class object gives us access to the various characteristics as well as the ability to create new objects of that class.

Additionally, a Class serves as the template from which unique objects are made. The same type of thing can be found in large quantities in the real world. like autos. the identical make and model (and all have an engine, wheels, doors, and so on). Each automobile contains the same parts and was constructed using the same set of blueprints.

class Human:“I am Human”age = 33def greet(self):print(‘Hello’)# Output: 33print(Human.age)# Output: <function Human.greet>print(Human.greet)# Output: “I am Human”print(Human.__doc__)

Output

33<function Human.greet at 0x7fc78c6e8160>I am Human

Creating an Object

We discovered that the class object might be utilised to access various characteristics.

It can also be used to create new object instances of that class (instantiation). The process of creating an object is comparable to calling a function.

>>> Arshad = Human()

This will result in the creation of a new object instance named Arshad. Using the object name prefix, we may access the attributes of objects.

Attributes can be either data or methods. An object’s methods are matching functionalities of that class.

This indicates that because Human.greet is a function object (a class attribute), Human.greet will be a method object.

Conclusion

These are basic concepts that I have covered for newcomers, and I must finish the following section shortly. For additional updates, ping me on Twitter.

--

--