Learn Python Basics in 10 Minutes!

Where do I learn Python?

How can I learn Python basics?

Can I learn Python? What is the best online source to learn Python?

These types of questions get very often asked daily on community forums like Quora and Reddit.

My answer to the big question of “How and when to learn Python” would be this:

Now.

Yes, learn Python at this moment.

In this article.

Do it before you get busy with the next thing you’re doing.


Here are the steps:

  • Download and install Python. Whether you are on a Windows, Mac or Linux, you can download Python from the Python official website. A good idea is to download version 3 of Python (e.g., Python 3.5.1). Once you’ve downloaded the latest Python, go ahead and install it.
  • Create and execute your first Python program.
  • Once you have installed Python, you can create your first Python program following these steps: Create an empty .py 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.
  • 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 located in a folder called test which 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 was programmed 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 be opened by typing this in the command line:

python

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


Next, learn variables

Variables are like containers where you can store every Python object. They are used to transporting 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’s how you create and access variables. Let’s learn some more stuff.


Learn the various datatypes you can use in Python.

When you created 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 e.g, 10.2

Lists e.g, [1,2,”Hi there”, 3]

Dictionaries {“Name”:”John”,”Profession”:”astrolog”,”age”:172}

All of them may have methods associated with them. For instance:

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

That will replace letter a with letter e.

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

dir("any string here")


Next, 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 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’s 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 you are creating. Indentation needs to be consistent. If you indented four spaces for the first line after def, you should 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.


Now it's time to 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 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))


Next, learn conditionals.

Conditionals are what make your program able to take decisions. They are expressions that evaluate a condition and execute something based on the 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’s a complete conditional block. The code is quite human-readable. Just pay close attention to the indentation. That’s 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 scripts of Python code.

You can certainly solve some trivial problems with Python now.

However, you are still a bit 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 library.

If you’re not sure what to do next, we would recommend taking the following 50-hour complete Python course, which teaches how to develop advanced applications with Python: Python Mega Course: Learn Python in 60 Days with 20 Apps.

The course covers web and desktop applications, web scrapping, data analysis and visualization, image processing and more, and you learn how to build the following 20 applications:


Recommended Course

Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.