DEV Community

Cover image for Python 101: Introduction to Modern Python
Flavian Kyande
Flavian Kyande

Posted on • Originally published at Medium

Python 101: Introduction to Modern Python

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 was released back in 1991 by 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 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, Mac and Linux.


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:

print('Hello World!')
Enter fullscreen mode Exit fullscreen mode

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:

# printing a planet greeting message
print('Hello World')
Enter fullscreen mode Exit fullscreen mode

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

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:

# assigning a variable to our greeting message
message = 'Hello World!'
# printing the message variable
print(message)
Enter fullscreen mode Exit fullscreen mode

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:

# print the data type of our message variable
print(type(message))
Enter fullscreen mode Exit fullscreen mode

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:

planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
Enter fullscreen mode Exit fullscreen mode

These are just some of the data types 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 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:

# operator to check if Earth is in the planets list
'Earth' in planets
Enter fullscreen mode Exit fullscreen mode

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 in Python to store each planet’s size and print the planets larger than Earth by using conditions in the code below:

# 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)
Enter fullscreen mode Exit fullscreen mode

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:

# 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)
Enter fullscreen mode Exit fullscreen mode

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:

# 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)
Enter fullscreen mode Exit fullscreen mode

The size of Mercury which is 2240, will be printed. You can learn more about classes on the W3 Schools website.

Top comments (2)

Collapse
 
brayan_kai profile image
Brayan Kai

Woooow a really great article 🥳🥳 , Keep up 🤗

Collapse
 
flaviankyande profile image
Flavian Kyande

Thank you! 😊