Python beginnerâ€™s guide – Everything you need to know to get started
Python is an almost universally loved programming language that many developers profess to be their â€œfavoriteâ€� way to code. Thatâ€™s thanks to Pythonâ€™s clear and simple syntax, logical structure, and forgiven flexibility. All these things also ensure that Python is the perfect choice for beginners and remains one of the quickest languages to learn.
Then thereâ€™s the flexibility and usefulness of the language. Python is often listed among the top programming languages frequently sought after by employers, and this demand is expected to grow due to its role in machine learning, data science, and cyber security. Python is also popular for web development and is a fantastic learning tool.
If youâ€™re willing to do a little extra leg-work, you can even use Python for game development, mobile app development, and more.
In this post, youâ€™ll find everything you need to know about Python and how to get started. Weâ€™ll provide detailed instructions on how to get set up, how to build your first app, and where to go once youâ€™re ready to learn more.
What is Python?
Python was introduced in the 1980s by a development team led by Guido van Rossum at Centrum Wiskunde & Informatica in the Netherlands. Rossum was very much responsible for the philosophy and development of the project, and would go on to dub himself Pythonâ€™s â€œBenevolent Dictator for Life.â€�
More recently, Rossum stepped down from this self-appointed role, handing responsibility to the Python Steering Council. Python was conceived as an alternative to the ABC language, with core tenants being â€œreadabilityâ€� and â€œsignificant whitespace.â€� This was a language designed from the ground-up to be clear, concise, and easy to get to grips with.
Also read: What is Python and how do you get started?
In technical termsâ€¦
Python is an interpreted, dynamically typed, garbage-collected, high-level, object-oriented, programming language.
Letâ€™s break that down.
As an interpreted language, Python code is run by a separate program installed on your machine, rather than being â€œcompiledâ€� into a format that the machine reads natively. This means you can run your Python code right from the terminal or command prompt without the additional step of first building it into an app or package (though there are ways to do this should you need to). This can save a lot of time when youâ€™re building a tool that you want to use right away!
â€œDynamically-typedâ€� means that you have to write less code for Python to know what you mean. While this example might not mean much to beginners, this means you donâ€™t need to explicitly define variable types in your code.
In programming, â€œgarbage collectionâ€� refers to memory management. Because Python is â€œgarbage collected,â€� that means it will reclaim memory as it runs the program. This makes life much simpler for the developer, as you would otherwise need to handle this yourself!
â€œHigh-levelâ€� means the code looks more like English than some other languages. Thatâ€™s because there is more â€œabstraction.â€� Even with no experience programming, certain lines and statements in Python will be self-explanatory.
And finally, Python is object-oriented (OOP) because it allows for the creation of classes and objects. This refers to how the code is organized and data is structured, which can lead to more efficient programs and convenient reusing of code between projects. The great thing about Python though, is that it doesnâ€™t force you into an object oriented structure. OOP is a tricky concept to wrap your head around as a beginner, so the fact you can start out with more basic sequences of statements is welcome. We, therefore, say that Python â€œsupports multiple paradigms.â€�
Also read: What is object oriented programming?
What can you do with Python? (What Python is and isnâ€™t good for!)
When you first start Python development, you will initially be pleased at how simple it is to perform simple processes. This is a great feeling for a new developer, and it makes Python a perfect language to get your feet wet with.
The issues come when you try to share your projects. Because Python is interpreted, this means you can initially only share your projects as Python files: code that requires an interpreter and some experience in development to run. You canâ€™t just email your program to a friend for them to try it out!
So, what do you do with this Python code?
One option is to create a web app. Many of the biggest and most influential websites and tools on the net were built using Python. These include:
This works because the Python code is run on the server. That is to say, a computer in a warehouse somewhere runs the Python code then uses this to change the layout of a website. That website is then shown to the user when they point their browser at the correct URL. Because the code is run on the server (â€œserver-sideâ€�) and not the userâ€™s computer, there is no need for them to have the interpreter installed!
To build these web apps though, you canâ€™t rely on Python alone. You will also need a framework such as Flask. Flask provides ready-made functionality to help you perform common tasks necessary for web development. Another option is Django.
It would also be useful to know some HTML and CSS, in order to handle the website UI, and perhaps some MySQL for storing and retrieving data.
Also read: How to use SQLite for Android app development
If you want to learn a little more about how you would go about building a web app, you can learn the basics in our guide to running Python on any platform.
Can you make mobile/Windows apps with Python?
The other option is to use external tools that will build your code into a portable app format. For example, you can convert your Python code into an .exe to run on Windows, or an APK file to run on Android.
Buildozer, for example, is a tool that will package Python projects as APK files for Android, or .IPA files for iOS. You can find out more about Buildozer here. You will likely also want to use a library like Kivy which will provide the graphical UI elements that we expect from mobile apps.
We wrote a guide to building your first mobile app with Python and Kivy, which you can read here.Â If you want to create an executable file for Windows, then you can use additional tools like Pyinstaller.
Whatâ€™s important to keep in mind though, is that Python is not an officially supported option for creating Android or iOS apps. Neither is it particularly well-suited to developing software for Windows or Mac. The external tools listed here do not have graphical interfaces, and they leave a lot of the work to you as the developer. Chances are that the first time you try to build an APK with Buildozer something will go wrong.
While combining Python with Kivy and Buildozer for Android development has the advantage of being cross-platform, there are other, much simpler and more powerful, cross-platform development tools out there: such as Xamarin.
Also read: How to make an Android app with Xamarin
If you want to build Android apps exclusively, it makes a lot more sense to learn either Kotlin or Java to use with Android Studio. Interested in going that route? Hereâ€™s a guide on how to set up your first project.
If youâ€™re looking at iOS development, youâ€™ll want to learn Swift and Xcode IDE. You can learn more about that process in our iOS dev beginnerâ€™s guide.Â
The only real reason to use Python to develop apps on these platforms is if you absolutely love Python, you only know Python, or you have already developed something amazing with Python and you want to quickly share it to other platforms.
Can you make games in Python?
Pythonâ€™s relationship with game development is like its relationship with mobile development. It is technically possible to create games in Python, but this is far from the optimal solution.
The best way to create games in Python, is with another external tool called Pygame.
Pygame will make several things easier by providing ready-made code to draw shapes and colors to the screen, play sounds, etc. What Pygame does not provide though, is 3D rendering, ready-made physics, support for controller-input, or anything else of that nature.
That means you still must code all of this yourself: from how objects fall depending on where they are in relationship to the ground, to how quickly the protagonist accelerates when you press the left key.
It also means that you canâ€™t build 3D games with Pygame and will instead need to use something like Panda 3D, which is not easy to get started with. Most games built in Python will look a little basic and spend much longer in development. You then have the headache of trying to port those creations to other platforms!
A far easier and more powerful option for cross-platform game development is Unity. Weâ€™ve written about this at length in our Unity beginnerâ€™s guide.Â
Python for professionals
Other than web development, the primary use for Python is in building tools and software that you yourself will use. As an information security analyst, you might use a Python script to try and crack passwords for example. As a data analyst, you might use Python to store and retrieve large amounts of information. I personally wrote a script to search my Word documents for keywords a while back.
It doesnâ€™t matter if this Python code canâ€™t be easily shared, because you built it for your own use. Likewise, Python is popular for rapid prototyping.
And of course, itâ€™s great for learning!
How long does it take to learn Python?
The answer depends very much on what it is you want to learn. If you want to learn Python for web development, this will take a significant amount of time and effort. If you just want to familiarize yourself with the basics of programming in Python, you can learn the ropes in a few days!
Many people mistakenly think that programmers â€œlearnâ€� an entire language and every single tool associated with that language. In truth, most developers are in a constant state of learning and updating their knowledge. Each time we are tasked with learning something new, we need to familiarize ourselves with new packages, frameworks, or tools. Often developers borrow code from other users withoutÂ reallyÂ understanding how it works, or reverse engineer things that they have used in the past.
The best way to start learning Python is to choose a simple project (make a calculator for instance) and attempt that. Youâ€™ll learn the skills necessary for that project along the way. Once youâ€™ve done that, add some more features, or try something a little more complex. This will structure your learning, and youâ€™ll find that you pick up the skills you need for yourÂ goals in no-time.
Alternatively, why not try an online Python course? These courses provide a full education that will take you from beginner status to pro. Theyâ€™ll include projects to help you get started, as well as support, and tests. Some will even prepare you for exams that will provide industry-recognized certification. Android Authority readers get major discounts on popular Python courses, meaning you can get access to courses worth thousands of dollars for around $30-$40! You can find a full list of our recommended courses in our course guide.Â
Getting started with Python â€“ setting up
Now you have a good idea what Python is good for â€“ and what it isnâ€™t â€“ you are ready to get started!
To program with Python, you will need two components:
- The Python interpreter
- An editor or IDE
You also must familiarize yourself with the concept of modules and â€œpip.â€� And if you like, and youâ€™re on Windows, you can add Python to PATH.
The most important part is the interpreter. By now, you know that the interpreter is the software that runs on your computer and â€œtranslatesâ€� Python code in real-time.
Imagine that youâ€™re in The Matrix and you want to learn German, so you stick that tube into the back of your head and you download everything you need to know. Now you can understand anything written in German! This is what the interpreter is for your PC and Python.
You can download the interpreter (referred to as â€œPythonâ€�) here.
When downloading Python, there used to be the question whether to get the latest version (3.8 at the time of writing) or the older Python 2.7. There are syntax differences between Python 2 and 3 that mean not all Python code will run on each version. For this reason, many organizations that had invested vast amounts of time developing projects in Python 2 did not want to make the jump. Likewise, many crucial external tools would only support Python 2 (including Pygame at one point!).
However, Python 2 has since lost official support, and by now most organizations and developers have finally made the jump. For that reason, 99% of people should download the latest version of Python 3. This is still worth knowing about, however, in case you ever run into problems trying to use a module or library that only works on the previous iteration!
You can learn more about this in our updating guide.Â And if you want more detailed instructions to help you install Python on Windows, Mac, or Linux than youâ€™ll want to head to our install guide.Â
Python editors and IDEs
Our installation guide will also talk you through the process of choosing and installing an IDE/editor. When you install Python, it will only come with a very basic editor called the â€œShell.â€� While you can write Python code in here and run it, it wonâ€™t highlight mistakes or allow you to easily juggle multiple projects.
An IDE is an â€œintegrated development environmentâ€� that provides access to all the useful tools and features you could need, while at the same time providing powerful formatting, highlighting, and tips for writing your code.
Two of the best options, and the ones I personally use, are PyCharm andÂ Visual Studio. Both these options are free for casual use.
As an aside, if you just want to start experiencing Python programming and have an Android or iOS device, you can also get some simple and cheap editors that will work out-of-the-box:
- iOS:Â Python3IDE
- Android:Â QPython 3L
PIP and installing modules
One of the most important aspects of Python programming, is learning how to use modules and libraries/packages. Modules are bits of code that contains â€œdefinitions and statements.â€� Essentially, these are bits of code that have been written by other people, that you can then refer to in your own code in order to perform powerful operations.
For example, a Python module I use often is python-docx. This module makes it easy to open, parse, and display .docx files (Word format) or to create new ones.
Also read: How to write to a file in Python â€“ Txt, Docx, CSV, and more!
A package is simply a collection of modules, which must contain an __init__.py file. These provide lots of functionality, usually tied together with a common theme. An example would be Kivy! Libraries are collections of packages.
In order to use modules, packages, and libraries, you will usually use a tool called PIP.
PIP is a package-management system and comes included with your default Python installation. You use PIP from the terminal or CMD. With PIP installed, downloading python-docx is as simple as typing the following command:
python â€“m pip install doc-x
Note that if you want to be able to run Python commands from the command prompt on Windows and use PIP, then you will either need to open the command prompt in the same folder as your Python installation, or you will need to add Python to PATH so that it can be accessed anywhere.
How to use Python
Now that you have Python on your machine, an IDE or editor to type into, and a familiarity with how to add new modules via PIPâ€¦ whatâ€™s next?
Next we write some basic code!
The first program that we normally type out when beginning any new programming language is one that prints â€œHello World!â€�
We do this like so:
This will output the words â€œHello world!â€� to the terminal.
There are lots of little tricks you can use when printing to the screen in Python, so check our guide on how to print in Python for more details.
The next thing to learn about in any new programming language is variables. Variables are â€œcontainersâ€� that we can use as stand-ins for values and data. First, we assign some value to an arbitrary name. Then we can use that name whenever we want to refer to that information:
greeting = "Hello World!" print(greeting)
This comes in very handy if you need to refer to some information throughout your code. Or, more likely, if you want to change that data to trigger changes throughout your program.
Variables can come in different â€œtypes.â€� For instance, a variable that consists only of whole numbers is called an â€œintegerâ€� or â€œintâ€�. Numbers that need decimal places are called floats or doubles. Sequences of alpha-numeric characters are called strings (â€œHello world!â€� is a string).
Related: How to use Strings in Python
Because Python is dynamically typed, that means that we donâ€™t need to choose what type of variable weâ€™re going to use when we introduce it.
Lists and dictionaries are even more powerful, as they allow you to store multiple values. You can learn more about these here:
There are specific naming and formatting conventions in Python that suggest the way you should name your variables and functions, and the way you should lay everything out. For example, variables and functions will typically use â€œsnake caseâ€� meaning that they are written in lowercase with underscores separating each word. You can learn more by referring to the PEP 8 style guide.
Also read:Â How to comment in Python: tips and best practices
How to use Python if statements
Once you have familiarized yourself with these basics, you can begin to control the flow of your programs. You do this using an â€œif statement.â€�
If statements let you check if something is true, and then show a segment of code only if it is.
name = "User" if name == "User": print("Access granted!")
This will now show the words â€œAccess Grantedâ€� on the screen, but only if the variableÂ nameÂ has the value â€œUserâ€�. Try changing that string to anything else, and youâ€™ll see that the program exits without displaying anything on the screen.
Notice that the code we want to show after the check is indented following a colon. This is what you call a â€œcode block.â€� Indentations are used to create code blocks like this any time we want to group together some code. Once the indentation ends, the statements will be considered part of the main â€œflowâ€� of code.
In this following example, the words â€œAnywayâ€¦ how are you?â€� will show on the screen regardless of whether access was granted.
name = "User" if name == "User": print("Access granted!") print("Anyway... how are you?")
Functions and classes in Python
Another basic skill to learn when using Python, is learning to call functions. Functions are code blocks that you can summon at any other point in your code. This can be useful if there is a function that you repeat frequently. To define a function, we use the statementÂ def. This looks like so:
name = "User" def hello_function(): print("Access granted!") if name == "User": hello_function() print("Anyway... how are you?")
Itâ€™s also possible to pass a variable or variables into a function from within your code. These are called â€œarguments.â€� You can learn more about using functions and arguments in our functions guide.
A class is like a function but allows you to create an â€œobject.â€� This object can have its own properties and functions which can then be retrieved or called from anywhere in your code. The class will act as a blueprint, allowing you to create multiple versions of that same object, each with their own properties.
class MyClass: x = 5 classy = MyClass() classy2 = MyClass() classy2.x = 7 print(classy.x) print(classy2.x)
Here, the class â€œMyClassâ€� is used in order to build an object that has a single property: x. We then create two instances of the MyClass object and change the value of â€œxâ€� for just one of them.
Continuing your education
We have only scratched the surface here in terms of what Python is capable of and what can be done with it. There are plenty more articles on this site that can help you take your understanding further! Alternatively, why not try one of our recommended online Python courses? You can get a complete education in Python for as little as $37!
Stay tuned for much more on Python from us here at Android Authority. And good luck! Python is a wonderful language to learn, and with each new statement you understand, youâ€™ll create limitless new creative possibilities!