Where do I learn Python? How can I learn Python basics? Can I learn Python? What is the best online plasource to learn Python?
These types of questions get very often asked day on community forums like Quora and Reddit.
My answer to the big question of “How to learn Python” would be this:
Yes, learn Python in 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 file and inside the file type in this:
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:
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:
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:
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.
- Create an empty file and inside the file type in this:
- Next, learn variables
Variables are like containers where you can store every 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:
Or just do some math:
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)
Lists ([1,2,”Hi there”, 3])
n”:”astrolog”,”age”:172})All of them may have methods associated with them. For instance:
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:
dir("any string here")
- Next, learn to create custom functions.
There are two types of functions in Python – built-in and custom.
Print() is a 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’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:
There we printed out the function output, but you can do whatever you like with it:
That will multiply the function output by 20 and store it inside the product variable.
- Then, 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))
- Now 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 21-hour complete Python course which teaches how to develop advanced applications with Python.
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 10 applications:
As a reader of PythonHow you can get get the course on Udemy for $19 instead of its $195 list price: