Python 101: Introduction to Modern Python
Tags: python, codenewbie, beginners
Published: 2022-02-14 17:17:41

## Brief Python Introduction
I have been lucky to jump into the Python bandwagon this year and I can feel the enthusiasm and support the Python community has had to offer to learners like me. Having gone through the basics, here is what I could gather from learning Python that may help anyone willing to start their Python journey.

**[Python](https://en.wikipedia.org/wiki/Python_(programming_language))** was released back in 1991 by **[Guido van Rossum](https://en.wikipedia.org/wiki/Guido_van_Rossum)** with the intention of being as powerful as the other major languages, having an easy syntax thus making it understandable as plain English and open source. It is fair to say that it achieved its objectives and now it is one of if not the most popular programming language.
Python is easy to install, set up and begin to code with. Python is used extensively in data science, web development, games, pc apps, business systems and many more areas in the technology industry.

---


## Installing Python
Python has multiple ways for you to install on your machine. It depends on what is suitable for you and also, your computer specifications play a big part. It is always better to research the specifications required before installing.
It also depends on your career path. If you are in the data science field like me, **[Anaconda](https://www.anaconda.com/products/individual)** would be a suitable option for you. This means there is no one fits all approach in installing python, that option solely lies on you. You can also check the official Python website for specific download versions, for **[Windows](https://docs.python.org/3/using/windows.html)**, **[Mac](https://docs.python.org/3/using/mac.html)** and **[Linux](https://docs.python.org/3/using/unix.html)**.

---


## Getting Started with Python
In this article, we will be using the planets to go through the basic concepts of Python. We will start with the commonly used code line all over, greeting our planet Earth using the print function:

```python
print('Hello World!')
```
After running this code, the output will be ‘Hello World!’. Congratulations! You have successfully run your Python code.

---

## Comments
Comments are vital to coding. They help the user to understand their code better which can be advantageous in your learning process. Commenting also helps other developers read your code with ease. When you comment on your code it is a win-win situation for everyone who will interact with your code which you’ll notice going through this article.
In Python, the comments are made using the hashtag or pound sign (#) before the comment line. You can also you the keyboard shortcut `ctrl + /` to comment. We will start by commenting on our previous print statement:

```python
# printing a planet greeting message
print('Hello World')
```
Now, when anyone revisits your code including you, they will have a gist of your code.

---

## Variables
Variables are the basic unit of a programming language. These are used as a reference to an object. They store values assigned to them for use in the program. Variables take the last value assigned, so it is wise to use different variable names for different values in your code.
The following convention is used in the Python variable naming:

![Image from Naming Convention](https://miro.medium.com/max/1400/1*dKMkqOxRxEraIMBj8usWfA.png "Image from Naming Convention")

You can assign values to variables in Python without declaration. Continuing with our planet greeting, we can assign the greeting to a variable as such:

```python
# assigning a variable to our greeting message
message = 'Hello World!'
# printing the message variable
print(message)
```

---

## Data Types
A data type is a classification of values that can be stored in a variable. Some of the most used data types in Python are strings, numbers (integers and floats), boolean, lists, tuples, sets and dictionaries.
You can get the data type of a variable by using the `type()` function on any object. Let’s apply this in the previous variable we had:

```python
# print the data type of our message variable
print(type(message))
```

The output will be `<class ‘str’>`, signifying that the data belongs to the string type.
We can also create a list for our planets in Python:

```python
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
```

These are just some of the **[data types](https://docs.python.org/3.10/library/datatypes.html)** available in Python.

---

## Operators
Operators are used to telling the program to perform either arithmetic, logical, assignment or comparison operations on variables and values.
We’ve already used an assignment operator above to assign values to variables with the equal sign (=).
**[W3 Schools](https://www.w3schools.com/python/python_operators.asp)** explains these operators with some code examples.
We can check if our planet Earth is in the planets list we created above through the code:

```python
# operator to check if Earth is in the planets list
'Earth' in planets
```
This will print True since as the Membership operator `in` explains, it returns True if a value is contained in a set of values.

---

## Conditionals
This is a powerful tool in programming that places two or more variables against each other under set conditions to returning either true or false. Conditionals work hand in hand with most of the operators above to return a value that meets the conditions stated in a program.
In our planets data, we can create a **[dictionary](https://www.w3schools.com/python/python_dictionaries.asp)** in Python to store each planet’s size and print the planets larger than Earth by using conditions in the code below:

```python
# a dictionary that has planets and their radius size in km
planet_dictionary = {
  'Mercury': 2440,
  'Venus': 6052,
  'Earth': 6371,
  'Mars': 3390,
  'Jupiter': 69911,
  'Saturn': 58232,
  'Uranus': 25362,
  'Neptune': 24622
}
# condition for looping through the dictionary values and printing planets larger than earth
for i in planet_dictionary:
  if planet_dictionary[i] > planet_dictionary.get('Earth'):
    print(i)
```

This will print out Jupiter, Saturn, Uranus and Neptune since they are larger than Earth.

---

## Functions
A function is a block of reusable code that is used to perform a single task. There are two types of functions in Python, built-in and user-defined functions
Built-in functions come in predefined in the program. We have been using `print()` and `type()`, which come with the Python program, that helps us perform useful actions.
User-defined functions are functions created by the user to help in achieving specific tasks. In Python, user-defined functions begin with the word def.
We will create a function that can be used to perform the task above so that we wouldn’t have to type it again when we need to reuse it for other objects:

```python
# function that outputs planets larger than than user's planet
def larger_than_planet(planet, dictionary):
  for i in dictionary:
    if dictionary[i] > dictionary.get(planet):
      print(i)
# Looking for planets larger than Neptune
larger_than_planet('Neptune', planet_dictionary)
```

The code above will return Jupiter, Saturn and Uranus. This is our own user-defined function that enables us to input our planet of choice and returns planets larger than our choice.

---

## Classes
Class is a model of an object that defines or specifies all the properties of objects. Here we have two terms, object and class, which we have to break down.
Going back to our planets example, we can say that the planet is our model or blueprint. It helps us define the properties (size, distance from sun) that we require in each of the planets (object).
In Python code, this would be:

```python
# creating a Planets class
class Planets:
  # adding atttributes to our class
  def __init__(self, planet, size, dist_from_sun):
    self.planet = planet
    self.size = size
    self.dist_from_sun = dist_from_sun
# adding a planet instance to our class
first_planet = Planet('Mercury', 2240, 58)
# printing the size of the first planet
print(first_planet.size)
```
The size of Mercury which is 2240, will be printed. You can learn more about classes on the **[W3 Schools website](https://www.w3schools.com/python/python_classes.asp)**.