Letâ€™s start from the beginning: what is Python and why should you learn it?
Python is one of the worldâ€™s most popular programming languages. It powers a huge number of extremely influential apps and websites, including Instagram, Google, Spotify, and Netflix. Python is also commonly used in data science and machine learning, which makes it a very â€œfuture-proofâ€� language and one that is likely to stay in-demand for a long time.
Python powers a huge number of extremely influential apps and websites, including Instagram, Google, Spotify, and Netflix.
Despite its clear power and flexibility though, Python is also one of the most beginner-friendly programming languages youâ€™re likely to come across. Python serves as a fantastic â€œgateway drugâ€� into the world of coding, and offers a gentle introduction to higher-level concepts such as object-oriented programming.
Python is also one of the most beginner-friendly programming languages.
Development on Python began in the 1980s, led by Guido van Rossum at Centrum Wiskunde & Informatica in the Netherlands. This was very much Guidoâ€™s brainchild, and he even dubbed himself the languageâ€™s â€œBenevolent Dictator for Lifeâ€� (BDFL) though he would step down from this role in 2018, passing responsibility on to the Python Steering Council instead.
What is Python in programming terms? Python was conceived as an alternative to the ABC language. It is an interpreted, dynamically typed, garbage-collected language that supports numerous paradigms (object-oriented, procedural, functional).
If youâ€™re just starting out, you donâ€™t need to worry about any of this. Just know that Python is relatively simple to learn, but also highly in-demand and very powerful. Letâ€™s take a look at how to get started with it and how to build your first, very simple, app.
How to get started with Python
First things first, you will need to download some software to use to start programming in Python.
If youâ€™re on a desktop computer, that means two things:
- A Python interpreter
- A Python IDE
What is a Python interpreter? This is the software that reads the Python code and runs it. Installing an interpreter is like teaching your computer how to speak a foreign language.
The IDE meanwhile, is the â€œIntegrated Development Environment.â€� This is the program that you will use to actually type your Python code into. You can save and open files this way, and all on the interpreter when you want to run it. This is your interface for Python development.
When installing an interpreter, you need to decide whether youâ€™re going to choose Python 2 or Python 3. Each version has pros and cons, but Python 2 is no longer officially supported, making Python 3 the future-proof choice.
(If you were wondering â€œwhat is Python not so good forâ€� one answer is that it is fragmented in this manner, which can present a little bit of confusion getting started!)
Download the latest Python interpreter here:
Note that you may already have a Python interpreter installed, especially if you are running MacOS or Linux.
When it comes to the IDE, there are a number of good options to choose from. Some of these are free, others will cost money but offer advanced features. Some good options include:
PyCharm is free and is among the most popular options for Python development. It is the tool I recommend for most users. That said, it can be a little complex to set up, so be sure to follow the official documentation here.
On mobile, things are simpler because the IDE and interpreter are built into a single app. This is a great way for beginners to get started.
To start coding on mobile, youâ€™ll find a single app and download it. Two good examples for learning the ropes are:
There are other versions available with a range of payment models. Both these are good choices for getting started free though.
Once you have any of these things installed, youâ€™re ready to write your first Python program!
Python 3: Hello World
It is tradition when learning any new programming language, to start by writing a piece of code that simply writes â€œHello Worldâ€� to the screen. To that end, you will need to use the following code:
Now hit â€œPlayâ€� and you should see the text appear on the screen.
Side note: If you were using Python 2, you wouldnâ€™t need the brackets.
Letâ€™s move quickly onto the next lesson: what is a variable in Python?
A variable is like a container that can be used to represent a number or a piece of text. We define this in the code by simply writing a word and then giving it a value.
For example, you could say:
MyVariable = â€œHi there!â€� Print(MyVariable)
You will see the message â€œHi there!â€� appear on the screen. Notice that you donâ€™t need the quotation marks to print a variable, quotation marks are interpreted literally.
A group of letters like this is known in programming as a â€œstring.â€� This is one type of variable, but there are many others. Another type of variable is an integer. This is a variable that represents a whole number.
So we could also say:
MyVariable = 3 Print(MyVariable)
Which would print the number 3 onto the screen!
Some other languages would require you to specifically state what type of variable you wanted to use (String MyVariable = â€œHello!â€�), but in Python, the interpreter figures that out from context. Part of the reason this is possible is that Python uses a smaller number of variable types as compared with, say, Java. There are no â€œBooleansâ€� in Python for example.
While we wonâ€™t need to deal with other data types right now, you will eventually encounter the following variables in Python programming:
Manipulating data in Python
Why would you ever want to use a variable? Because it makes your code dynamic. It means that you can alter the way your program behaves depending on the action of the user, and depending on various other factors.
Try running this code and see what happens:
MyVariable1 = 2 MyVariable2 = 20 Print(MyVariable1 * MyVariable2)
Hereâ€™s a clue: in computer-talk, the * symbol represents multiplication.
You can also combine strings in interesting ways:
FirstName = â€œBillâ€� LastName = â€œGatesâ€� FullName = FirstName + â€œÂ â€œ + LastName print(FullName)
Your first Python 3 app!
So now weâ€™ve answered the question â€œwhat is Pythonâ€� and weâ€™ve had a go at writing some basic code.
What about making something that a person might actually be able to use and have fun with?
Also read:Â Kotlin vs Java for Android: Key differences
To do this, weâ€™re going to need to let the user interact with the program. That means we need to handle input.
UserName = input(â€œPlease enter your name: â€�) Print(â€œHello â€œ + UserName)
You can probably figure out what is going on here! When you use the command â€œinput,â€� Python will prompt the user with the text in the brackets, and then wait for the response. That string will then be referred to as UserName.
Note: Python 2 uses raw_input instead of input.
Now we have inputs, the ability to talk to the user, and even some basic math. How about we put this together in a fun little app? This one will tell you how long you have until you are 100 years old!
UserAge = input("How old are you? ") YearsTo100 = 100 - int(UserAge) print("In ", YearsTo100, "years, you'll be a hundred!!") print("That is ", int(UserAge) * 360, " days! Or ", (int(UserAge) * 360) * 24, " hours. ")
There is one last trick that I want to share with you before we go: using conditional statements.
A conditional statement is a command that only runs under certain conditions. This usually means checking the value of a variable first.
To use a conditional statement in Python, you use the â€œIfâ€� statement, followed by an indentation.
UserName = input(â€œPlease enter your name:â€�) print(â€œHello â€œ + UserName) if UserName == â€œAdamâ€�: Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â print(â€œAdmin mode enabledâ€�) print(â€œWhat would you like me to do now?â€�)
In this program, the indented code will only run if the UserName given is Adam. Note that when checking a value as opposed to assigning one, we use two = signs rather than one.
Here then, the user will be asked what they want to do next whoever they are â€“ but only I will be granted admin status. Or other people called Adam.
Weâ€™re just scratching the surface of Python can do
Using these basic commands and lessons, you can actually do an awful lot already. You could make a quiz, a calculator, a simple database, and more! To really flex the full power of Python though, youâ€™ll need to understand concepts such as functions, modules, and more. To that end, we recommend checking out our guide to the best online Python courses.Â
That said, if youâ€™re a true beginner and looking for a great course thatâ€™s easy to get started with, we highly recommend Coding with Python: Training for Aspiring Developers, which you can nab for just $49.99, which is an absolute steal as the course is valued around $700.