Python beginner’s guide – Everything you need to know to get started

Python beginner’s guide – Everything you need to know to get started

Python Code

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.

Coding in Python and Java

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!

See also: How to run Python apps on any platform

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:

  • Google
  • Instagram
  • Spotify
  • Netflix
  • Uber
  • Dropbox
  • Pinterest

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!

Typing Writing Coding Keyboard

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.

See also: How to make a game in Python: An introduction to Pygame

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.

What is Unity Development

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.

Python Version in Terminal

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!).

See also: What is Python? Get started with your first app!

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.

Adroid developer, development, programming

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:

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:

print("Hello world!")

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.

For example:

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.

For example:

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

Python Tutorial

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!


For more developer news, features, and tutorials from Android Authority, don’t miss signing up for the monthly newsletter below!

Stay up to date on all things Android Development

Join our developer crew to get a monthly developers newsletter curated by in-house experts!

By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.


source https://www.androidauthority.com/learn-python-beginner-1142688/
Previous Post
Next Post

post written by:

0 Comments: