Python tutorial for total beginners: Build a project from scratch
Tags: python, tutorial, beginners, codenewbie
Published: 2023-03-01 18:31:33

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 languages 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 our [Absolute Beginner’s Guide to Programming.](https://www.educative.io/blog/learn-how-to-code-beginners-guide?eid=5082902844932096)*

**Today we will cover:**
* [What is Python?](#what-is)
* [Python terms and syntax](#terms) 
* [First steps with Python: build a program](#build) 
* [What to learn next](#next-steps)

<br>

<a name="what-is"></a>

## What is Python?

![Python](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8q0h6g8ddflg4sot5ya4.png)

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. 

<br>

### Popularity of Python

Python remains one of the most popular programming languages around the world. 

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. 

<br>

### `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.

```py
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 below for how to produce "Hello World" in Python.

```py
print("Hello World")
```

<br>

<a name="terms"></a>

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

<br>

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

<br>

### Numbers 

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

<br>

* **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`)

![Numbers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bm4yzjgeymqy3871w3kh.png)

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

![Strings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mb69r270q0eq42j1bnyb.png)

```py
random_string = "I am Batman"  # 11 characters
print(len(random_string))
```

**Output:** 11

<br>

### 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 

<br>

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

![Functions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hppjbxmghvd87equy45e.png)

The syntax for creating a function is as follows in Python:

```py
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. 

<br>

### 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`.

<br>

### 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 $nth$ 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 `[ ]`.

```py
thislist = ["banana", "carrot", "cherry"]
print(thislist) # print all items in a list
print(thislist[1]) # access and item by referring to its index number
```

**Output:** 

```py
['banana', 'carrot', 'cherry']
carrot
```

<a name="build"></a>

## 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

<br>

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

![Python Projects](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mo0x0vzdan9hsxs67xjq.png)

### 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! 

<br>

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

```py
print("Hello World!")
```

You can also print any other text that you wish to. Have the program print your name! Check out the code below to see how it works. 

```py
print("Hello World")
print("Your Name")
print("Welcome to Educative!")
```

<br>

### 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. Check out the examples below. 

```py
a = 88
b = 103
print(a + b)
```

**Output:** 191

### Step 5: Explore an if-statement with Python 

Let's use the concept of if-statements. The basic structure looks like this:

```py
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

```py
num = 5

if (num == 5):  # The condition is true
    print("The number is equal to 5")  # The code is executed

if num > 5:  # The condtion is false
    print("The number is greater than 5")  # The code is not executed
```

**Output:** The number is equal to 5

<br>

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

```c
This 
is 
a 
function
```

```py
def my_print_function():  # No parameters
    print("This")
    print("is")
    print("A")
    print("function")
# Function ended


my_print_function()
```

**Output:** 
This 
is 
a 
function

<br>

## 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.](https://www.educative.io/courses/learn-python-3-from-scratch?eid=5082902844932096)** 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 on Educative
* [Level up your Python skills with these 6 challenges](https://www.educative.io/blog/level-up-python-skills?eid=5082902844932096)
* [A Complete Guide to Web Development in Python](https://www.educative.io/blog/web-development-in-python?eid=5082902844932096)
* [How to learn Python in 5 easy steps (for beginners)](https://www.educative.io/blog/how-to-learn-python-in-5-easy-steps?eid=5082902844932096)

### Start a discussion
What language did you learn (or are currently learning) to jumpstart your learn to code journey? Was this article helpful? Let us know in the comments below!