Home/Blog/Learn to Code/Python tutorial for total beginners: Build a project from scratch
Home/Blog/Learn to Code/Python tutorial for total beginners: Build a project from scratch

Python tutorial for total beginners: Build a project from scratch

Amanda Fawcett
Aug 21, 2020
11 min read

Become a Software Engineer in Months, Not Years

From your first line of code, to your first day on the job — Educative has you covered. Join 2M+ developers learning in-demand programming skills.

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 when you are learning to code in Python.

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.

Become a Python developer for free.

Get a handle on the basics of Python and learn how to build your own projects.

Learn Python 3 from Scratch

What is Python?#

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:

  • Web and mobile app development
  • Mathematical analysis
  • Processing big data and data science
  • Desktop apps and software development
  • Writing system scripts
  • Video game development

Properties of Python#

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 and TOKEN are different variables. Lastly, Python is dynamically and implicitly typed. This means that a data type is enforced when we run the program.

Popularity of Python#

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 World in 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 print statement.

print (data)

Whatever we need to print is encapsulated in the parentheses following the print keyword, which is a command to the computer to print the text. Take a look and then try it yourself with the code tab.

print("Hello World")

Python terms and syntax#

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.

Data types and variables#

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:

  • Numbers: integers and digits
  • Strings: words in parentheses " "
  • Booleans: data is either false or true

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:

  • The name can start with an upper or lower case alphabet.
  • A number can appear in the name, but not at the beginning.
  • The _ character can appear anywhere in the name.
  • Spaces are not allowed. Instead, we must use snake_case to make variable names readable.
  • The name of the variable should be something meaningful that describes the value it holds.

Numbers#

Python is especially suited for manipulating numbers. There are three main types of numbers in Python:

  • Integers: all positive and negative whole numbers (0)
  • Floating Point Numbers: positive and negative decimal numbers (-15.7)
  • Complex Numbers: real and an imaginary numbers (8j)

Strings#

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 characters
print(len(random_string))

Conditional statements#

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

Functions#

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):

The 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.

Loops#

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.

  1. 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 for keyword.

  2. 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 1000.

Data structures#

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:

  • Lists: store elements of different data types in one container
  • Tuples: like a list, but cannot be changed (immutable)
  • Dictionaries: stores key-value pairs
  • Sets: an unordered collection of data items

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 nthnth 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 list
print(thislist[1]) # access and item by referring to its index number

Jumpstart your Python journey for free#

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.

Learn Python 3 from Scratch

First steps with Python: build a program#

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)
  • IDLE
  • Spyder

Step 1: Download Python and your IDE#

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.


Step 2: Create your first program#

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!

Step 3: Write the 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:

print("Hello World!")

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!

Step 4: Explore some math with Python#

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 88 and 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!

Step 5: Explore an if-statement with Python#

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:

  • The number is equal to 5
  • The number is greater than 5
# try it yourself

Step 6: Create a function in Python#

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

Next steps#

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:

  • Deep dive into Python syntax
  • Operators
  • Function scopes
  • Python Object Oriented Programming concepts
  • Using Dictionaries
  • Converting lists to tuples
  • Python libraries
  • Popular Python modules

To get started with these concepts and move onto these more advanced topics, We 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!

Happy learning!

Continue reading about Python#


  

Free Resources