If you are a coding novice and want to start your coding journey, you’ve probably heard of Python. Python is one of the most popular language out there, and it continues to rise in popularity in demand. It’s an ideal language because it’s intuitive and simple.
Today, we will go through a Python beginner’s tutorial and build a simple dice rolling project step-by-step. Most Python tutorials jump into libraries right away, but you need a solid understanding of the basic syntax first.
We will discuss the basics of Python that you’ll use throughout your career. No prerequisite knowledge is required, but to be most successful with this article, a basic understanding of programming is helpful. For a quick introduction or refresher, check out my Absolute Beginner’s Guide to Programming.
Today we will cover:
Get a handle on the basics of Python and learn how to build your own projects.
Python is a general purpose programming language that was first developed in 1990. It is one of the most popular programming languages around the world. The term “general-purpose” means that Python can be used for a variety of applications and uses.
Python is very intuitive and easy to use. Python is not only easy to pick up, but it is beloved by developers around the world.
Python can be used for things like:
Python is strongly typed, which means that the type of data in your program is enforced. Python is also object-oriented, which means that all the elements of your programs are object you can act upon and manipulate.
Like many programming languages, Python is also case sensitive. This means that capitalized letters are recognized as different elements than lowercase letters, i.e.
TOKEN are different variables. Lastly, Python is dynamically and implicitly typed. This means that a data type is enforced when we run the program.
Python remains one of the most popular programming languages around the world. In the 2020 StackOverflow survey, it ranks 1st for most popular language and 1st the most wanted language.
One reason Python is so popular is that important frameworks are written in Python, most notably machine learning frameworks. This means that Python’s popularity isn’t going anywhere, especially as data science become more commonplace across industries. Learning this language is an important step for becoming a professional programmer.
Hello Worldin Python
By now, we’ve learned the basics properties of Python. We are now ready to see some code. Whenever we learn a new language, it is an age-old tradition to start by displaying the text “Hello World” on the screen.
Since Python is one of the most readable languages out there, we can print data on the terminal by simply using the
Whatever we need to print is encapsulated in the parentheses following the
Now we’ve seen a bit of Python code. Let’s learn about the basics of Python, such as the main terms, semantics, and syntax of Python. Then, we will build a first program with Python below.
In programming, semantics refers to the meaning of a particular element. Syntax refers to the structure of a programming language.
The data type of an item defines the type and range of values that item can have. Data types have unique properties due to their classification. Python does not place a strong emphasis on defining the data type of an object. This makes it a lot simpler to use. There are three main data types we can use in Python:
A variable, on the other hand, is a name that we assign to a value. This allows us to give meaningful names to data. We use the
= operator to do so. There are a few rules when it comes to naming your variables:
_character can appear anywhere in the name.
snake_caseto make variable names readable.
Python is especially suited for manipulating numbers. There are three main types of numbers in Python:
A string is a collection of characters that close within single or double quotation marks. The most classic example is the famous string
Hello World. Every string created has an index and length associated with it. The index is a way of tracking the position of a character within a string. Length can be found using the
len statement in Python.
random_string = "I am Batman" # 11 charactersprint(len(random_string))
A conditional statement is a Boolean expression. Boolean means either true or false. With a conditional statement, a piece of code is only executed if something executers as either true or false. This is a great way to give your program some logic that controls the flow. There are three types of conditional statements:
If: if a desired result is not met, then terminate the program.
If-else: if a condition is true, execute the code. If not, execute another code snippet
If-elif-else: create multiple scenarios if the code does not execute as desired
A function is a reusable set of operations that performs a certain action on the code. Statements in our Python code will perform predetermined tasks. This allows us to reuse code and simplify our programs. In Python, there are built-in functions and user-defined functions that you make yourself.
The syntax for creating a function is as follows in Python:
def function name (parameters):
def keyword tells the program that we are defining a new function. You can name the function anything you want. The parameters, which are optional, are the inputs. This is how we pass data to the function. When we pass values into a parameters, we call them arguments.
A loop is a control structure that is used to perform a set of instructions for a specific number of times. Loops solve the problem of having to write the same set of instructions over and over again. We call this iterating over data. We can specify the number of times we want the code to execute.
There are two types of loops in Python.
A for loop uses an iterator to traverse a sequence of data. It begins at the beginning and goes through it until the condition is met. The loop always begins with the
A while loop continues iterating as long as a certain condition holds True. While this condition is true, keep the loop running. These are less common because they can loop infinitely and crash a program. For example, a while loop could find out the maximum power of
n before the value exceeds
A data structure is a way of storing and organizing data according to a certain format or structure. Python’s four primary data structures are:
Once you define and use a data structure, you can apply operations to your structures, apply them to other parts of your code, etc. For example, you could merge two lists or find the integer in a list.
Let’s see how to create a list. It is one of the most common data structures in Python. To do so, we use square brackets
thislist = ["banana", "carrot", "cherry"]print(thislist) # print all items in a listprint(thislist) # access and item by referring to its index number
Learn Python 3 for free with this interactive course, and get a handle on the most popular programming language in the world. By the time you’re done, you’ll have the skills you need to create your own basic applications in Python 3.
Now that we have a basic understanding of Python’s syntax and terms, let’s actually build a project.
For this project, we’ll be using Educative’s built in code editor. You can also follow along with a text editor or IDE of your choosing. Think of this like a Word doc where you write your program. The most popular Python IDEs are:
- PyCharm (recommended)
Python is free, open-source software that works on Linux, Mac, and Windows. It is preinstalled on Mac and Linux. It is recommended to use Python 3, the most current version.
You’ll need an IDE to make Python files. They are usually free. A popular text editor is PyCharm. If you’re not ready to download anything, follow along with Educative’s code environment.
To compile your code, you’ll need to download a Python interpreter as well. This isn’t necessary right away.
Open the PyCharm Editor and click on “Create New Project”. Rename the project to something like “First Project”. Click “Create”. Go to “File” > “New” > “Python File”. You now have a project.
A pop up will appear. Tye the name of the file you want, such as “HelloWorld” and hit “OK”.
Now we can write our first line of Python code!
To start writing our program, we have to make a Python file. The first line of our “Hello World” program will look like:
You can also print any other text that you wish to. Have the program print your name! Try it out below in the code widget.
# try out the Hello World program on your own# or print your own name!
Now that we have the Python program up and running, we can explore and try out some of the things we learned above. Let’s create some fun things!
Let’s try to do the following calculations with Python. Let’s try to add
103. We’ll have to define each as variables and then pint their sum. Try it yourself before checking the examples.
# Try it yourself here!
Let’s use the concept of if-statements. The basic structure looks like this:
if (condition == value): code to be executed
Remember: if the condition holds True, execute the code to be executed. Otherwise, skip it and move on.
Below, let’s try an example where we check the value of an integer. We need to provide a
num and then outline a way to test if that number equals 5 or if it’s greater than 5.
The code should return a
string that says either:
# try it yourself
Let’s create a function in Python now. Remember, we use. the
def keyword to declare a function.
Let’s write a function called.
my_print_function that prints four lines of text. This function won’t have any parameters. We also need. to print the function at the end. You can choose what those four lines will say. An example is:
This is a function
# try it yourself here
Congrats! You’ve now learned the basics of Python and explored some of those basics hands-on. You’re well on your way to becoming a talented Python developer. There is still a lot to learn. The next steps to take are:
To get started with these concepts and move onto these more advanced topics, I recommend Educative’s free beginner’s course Learn Python 3 From Scratch. By the time you’re done, you’ll have the skills you need to create your own basic applications in Python!
Join a community of more than 1.5 million readers. A free, bi-monthly email with a roundup of Educative's top articles and coding tips.