Python is a general purpose and very versatile language that’s easy to learn and can help you build almost anything you can imagine, from small command-line applications and utilities to big and complex machine learning applications for data science. In these examples we’ll go through the basics of Python in an interactive way.

First let’s define some variables and then we’ll check their type of variable.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
name = 'John Smith'
age = 25
school_average = 7.9
fav_fruit = ['mangoes', 'kiwis', 'bananas', 'oranges']
gym_routines = {
    'Monday': 'Jogging',
    'Tuesday': 'Leg day',
    'Wednesday': 'Biceps',
    'Thursday': 'Aerobics',
    'Friday': 'Yoga',
    'Saturday': 'Free day',
    'Sunday': 'Lazy day'
}

print(f"""
This is the type of all the variables we defined above:
    name is {type(name)}
    age is {type(age)}
    school_average is {type(school_average)}
    fav_fruit is {type(fav_fruit)}
    gym_routines is {type(gym_routines)}
""")

Result:

1
2
3
4
5
6
This is the type of all the variables we defined above:
    name is <class 'str'>
    age is <class 'int'>
    school_average is <class 'float'>
    fav_fruit is <class 'list'>
    gym_routines is <class 'dict'>

The previous commands showcase the way we can define variables and how we can check their type of variable, for that we’re using the built-in type function of python.

Practical example

No we will create a practical example, for this we will create a script that does the following:

  • Ask for a name from the user
  • Ask for their birth year
  • Show a greeting with the name the user typed in
  • Calculate and show their age based on the year they input and the current year
1
2
3
# Let's ask for the name first and assign it to a variable
name = input("Please type your name\n")
# We're adding '\n' at the end to add a new line in the input

By default the input function returns a string value, since the next piece of data we need is the birth year, we need to explicitly convert it to an integer using the built-in int function.

1
2
3
# We'll ask for the user's birth year and convert it to int
birth_year = int(input("Please type in your birth year\n"))
# Noticed that? We added the '\n' character again for a new line

To calculate the age of the user first we need to get the current year, for that we need to import the datetime module from the similarly named datetime library.

1
from datetime import datetime

Now this can be confusing but it’s just that the library and the module have the same time, but that’s not the only module in that library, there are many others that we might use in later examples. After the module is imported we just simply use the now() function in conjunction with the year function to get the current year.

1
current_year = datetime.now().year

After that we just substract the birth year from the current year to get the current age.

1
current_age = current_year - birth_year

After that we’ll just print the greeting with the user’s age.

1
2
print(f"Hello there {name}, I see that you're {current_age} this {current_year}\n
that's so cool!!!!")

If you put all the code together you get something like the following.

1
2
3
4
5
6
7
8
from datetime import datetime

name = input("Please type your name\n")
birth_year = int(input("Please type in your birth year\n"))
current_year = datetime.now().year
current_age = current_year - birth_year
print(f"Hello there {name}, I see that you're {current_age} this {current_year}\n
that's so cool!!!!")

Notice anything different? That’s right, we’re importing the datetime module first, this is due to Python coding conventions, all imports should be on the top of the file.

With this we’ve created our first python script, stay tuned for more examples and lessons.