Ardit Sulce

Where do I learn Python? How can I learn Python basics? Can I learn Python? What is the best online course to learn Python? These types of questions often show up on community forums like Quora and Reddit. So, how do you learn Python? Simple, you just start. Yes, learn Python at this moment. In this step-by-step article.

Do it before you get busy with the next thing you are doing. Once you get started, you will love it and want to explore all the Python libraries. If you are interested in web development, you might want to try the Python web frameworks that will speed up the development of web applications. If you are interested in data science, there are plenty of libraries that will help you crunch big data and give you answers.

But we are getting ahead of ourselves. First, let’s cover the basics of writing code in Python with these step-by-step instructions. Learn Python now through the following easy steps:

The Python Mega Course: Build 10 Real World Applications

Last Updated November 2020

  • 278 lectures
  • All Levels
4.5 (44,027)

A complete practical Python course for both beginners & intermediates! Master Python 3 by making 10 amazing Python apps. | By Ardit Sulce

Explore Course

Step 1: Download and install Python.

Whether you are on Windows, Mac, or Linux, you can download Python from the Python official website. A good idea is to download version 3 of Python.
Once you have downloaded the latest Python, install it.

Step 2: Create and execute your first Python program.

Once you have installed Python, you can create your first Python program following these steps:

  1. Create an empty file and inside the file type in this: 
print("It's working")

Name the file something like script.py where .py is the file extension associated with Python programs.

2. That’s it.

To execute your program, open the terminal/command line and type in python and the full path to your file. For instance, if your file is in a folder called test and is in your C drive, you would need to type this in your command line:

python C:\test\script.py

So, you’re telling your computer what program to use (i.e. python) to execute the file (i.e. script.py).
And that’s how you execute a Python program. You should now see the program output in the command line:

It’s working

As you see, this was just a simple script that you wrote to print out some text on the screen. Very basic, but that’s how you start.

If you just want to test things out, you can just type your code in the interactive Python shell which can you can open by typing this in the command line:

python

Be aware though that the code you type in the interactive shell will disappear when you close the shell. If you want to save the scripts, create a Python file as you did previously. The interactive shell is best suited when you are learning, testing, or exploring things.

Step 3: Learn variables

Variables are like containers where you can store every type of Python object. They are used to transport objects and values between different parts of the program. Start creating some variables and assigning some values to them. You can either type them in the interactive shell or inside a Python script if you like.

v = "Friday"
other_variable = 10
variable3 = 10.2
a_list = [1, 2, "Hi there", 3]

Want to do something with the variables you created? How about printing their values out? For instance:

print(other_variable)

Or just do some math:

print(other_variable + variable3)

So, that is how you create and access variables. Let’s learn some more stuff.

Step 4: Learn the various data types you can use in Python.

When we created the variables above, we assigned objects of various types to them because every object in Python has a certain datatype. Here are the main datatypes:

Strings (e.g. “Friday”)
Integers (e.g. 10)
Floats (10.2)
Lists ([1,2,”Hi there”, 3])
Dictionaries ({“Name”:”John”, “Profession”:”astronomer”, “age”:172})

All datatypes have methods associated with them. For instance, a string datatype might have a replace method among other methods:

"Friday".replace("a","e")

That will replace letter a with letter e and output the string “Friday”

To see a full list of methods that you can apply to an object, use the dir method and pass the object whose methods you want to explore:

dir("any string here")

Step 5: Learn to create custom functions.

There are two types of functions in Python—built-in and custom.
Print() is an example of a built-in function that prints out text on the screen. However, you may want to create your own custom functions for specific tasks. Here is how to create a function that gets euros and the current currency exchange rate as input values and returns dollars as function output.

def currency_converter(rate,euros):
    dollars=euros*rate
    return dollars

That is how you define a function.

Notice that the lines below the def line are indented with white space. When you indent lines, you are telling Python that those indented lines belong to the unindented line above them. In our case, all the indented lines belong to the function we are creating. 

Indentation needs to be consistent. If you indented four spaces for the first line after def, indent the same amount for the next line after that. A good practice is to indent four spaces.

Once you have created your blueprint—your function, you can generate outputs from it. Here is how you execute an instance of the function:

print(currency_converter(93, 1000))

There we printed out the function output, but you can do whatever you like with it:

product=currency_converter(93, 1000) * 20

That will multiply the function output by 20 and store it inside the product variable.

Step 6: Learn loops

You will soon run across scenarios where you will need to execute the same action multiple times. Let’s say you would want to generate multiple outputs of your currency converter function given a range of values. Instead of writing the function call multiple times, you could use a for loop instead:

for rate_value in [96, 94, 99]:
    print(currency_converter(rate_value, 1000))

In this example, we loop through the values 96, 94, and 99 that are in our list and run the currency_converter function on them.

Step 7: Learn conditionals

Conditionals give your program the ability to make decisions. They are expressions that evaluate a condition and execute something based on whether the condition evaluates to true or false.

V = 5
if v < 3:
    print("Less")
    print("Really")
elif v == 3:
    print("Equal")
else:
    print("Greater")

That is a complete conditional block. The code is human readable. Just pay close attention to the indentation. That is where most of the Python novices fail. A wrong indented line will throw an error and the execution will be interrupted.

Alright, now you know how to write simple Python scripts.

You can solve some trivial problems with Python now. However, you are still far from writing your first real-world program. For that you will need to practice the fundamentals you learned above and also learn to use third-party libraries that make available specialized code for specific coding activities. For instance, if you want to build websites with Python, you would need to learn how to use the Flask, Bottle, or Django library.

If you are not sure what to do next, I would recommend my 24-hour complete Python course “The Python Mega Course: Build 10 Real World Applications”. That course will teach how to develop advanced applications with Python. Good luck!

Page Last Updated: November 2020

Top courses in Python

2020 Complete Python Bootcamp From Zero to Hero in Python
Jose Portilla
4.6 (328,319)
Bestseller
REST APIs with Flask and Python
Jose Salvatierra, Teclado by Jose Salvatierra
4.6 (11,596)
Bestseller
The Complete Python Course | Learn Python by Doing
Codestars by Rob Percival, Jose Salvatierra, Rob Percival, Teclado by Jose Salvatierra
4.6 (11,703)
Python - The Practical Guide
Academind by Maximilian Schwarzmüller, Maximilian Schwarzmüller
4.6 (3,561)
Complete Python 3 Masterclass Journey
Jose Portilla
4.5 (2,482)
Automated Software Testing with Python
Jose Salvatierra, Teclado by Jose Salvatierra
4.6 (2,850)
Python Network Programming for Network Engineers (Python 3)
David Bombal, Experts with David Bombal
4.7 (4,858)

More Python Courses

Python students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo