DEV Community

Sarah Bartley-Dye
Sarah Bartley-Dye

Posted on

Intro to Python Module One: Getting Started with Python

I have been using SoloLearn more frequently lately to brush up on Python, so I can better teach it in Level M classes at Coding with Kids. Level M is the introduction to Python class. This is where students switch from Scratch to Codesters.

I’ve taught this class for a while, so it was about time to start a Python series. Since I’ve done the introduction to Python course on SoloLearn, I’m going to write a series about what I’ve learned in the course. SoloLearn’s Introduction to Python course is divided into six modules.

Each module is a mix of concept lessons, practice exercises (only available to Pro members), and a module quiz. At the end of the course, students earn a certificate. Today’s post starts with the first module, Getting Started with Python.

This module introduces the Python language. Students learn how to use the print command and variables. They will also learn about text and numerical data.

What is Python?

Before you can even start writing any Python code, it is a good idea to learn a little bit about Python itself. Python is a programming language, and SoloLearn considers it one of the most useful programming languages developers can use. This makes it a very popular language. 

When I started learning how to code in 2015, Python was talked about a little bit by developers. But it quickly became a fan favorite over the years and is now one of the languages suggested to new developers interested in learning how to code. Python is popular and useful like other programming languages, such as Ruby, because they are developer-friendly.

Python is a developer-friendly language because it is simple and easy to use. Python is a text-based language so it has readable syntax. That is what makes it easy to read, write code, and learn. That makes it a perfect place for new developers to start.

Different games and software use Python. Python has been used by Google and NASA, while video games such as Civilization IV use the language to create the game. Employers love Python as well and often look for developers who know Python, especially regarding data and AI.

Reminder: All code (regardless of what language you use) is instructions.

It is how we talk to the computer. So it doesn’t matter if it is Python, JavaScript, or Ruby. All the code is the steps we tell the computer how to do things. 

So you will need to think like a computer as you write code because computers think differently from how we do. You will need extra specific instructions to get your programs to work in any programming language. Apps and games are made of different statements.

A statement in programming is the instructions the computer reads and follows. Programs can vary in the number of statements they use, so always make sure you read through your statements carefully to catch errors before they happen.

How to display text in Python

The first command you will need to learn is the print() command. The print() command is how you get text on the screen. You will see this command a lot because developers use this command to show results and help them debug their code.

To use the print() command, you need to type print followed by parentheses. Inside the parentheses, you put your text. Take a look at the example below, where I put the text “Hello World” inside the print command.

print(“Hello World”)
Enter fullscreen mode Exit fullscreen mode

When you run this code, the computer will display this text on the screen. Before you press run on your computer, make sure you have your text wrapped in quotes as well as the opening and closing parentheses after the print keyword. These are common mistakes developers make when running code, so be sure to double-check your code for these common errors before you run your code.

Strings

When you put text inside of quotes, you are creating a string. Strings are in every programming language.  To create a string, you will wrap the text in quotes.

print(“Hello World”)
print('Hello World')
Enter fullscreen mode Exit fullscreen mode

You can use single or double quotes to make a string. The computer will run your code using either type. All the computer cares about is if the quotes match.

A common mistake developers make with strings is forgetting the quotation marks. If you forget one or both quotation marks, the computer will give you a syntax error. A syntax error is telling you that the computer is confused about what data type you are trying to use.

Numbers

Another data type you’ll use often with the print statement is numbers. To create a number in Python, you just need to type the number. If you want to print a number, you would use the print() command and put the number inside the parentheses.

print(3)
Enter fullscreen mode Exit fullscreen mode

You might see numbers wrapped in quotation marks sometimes in code. That doesn’t make them a number. Numbers wrapped in quotes are strings and will give you an error message if you try to use them in any calculations.

Number data means you can do calculations in Python, too. Look at the sample code below to see different statements using different math operations. When these statements are displayed on the screen, the computer will print out the answer.

print(7 + 3)
print(10 -5)
print(5 * 2)
print(10 / 2)
Enter fullscreen mode Exit fullscreen mode

Make sure you are careful when doing calculations. It doesn’t matter what operation you use. If you use a number with a number in quotes, you’ll get an error because you can’t do calculations with strings.

We will revisit numbers in future posts since numbers can be broken into integers and floats. The goal of this section is to print a number to the console and do basic calculations.

Variables

One of the first programming concepts SoloLearn introduces is variables. Variables can be found in any programming language because they help developers remember key information. You can think of them as digital versions of filing cabinets, boxes, or purses because they can hold any kind of information (i.e., strings, lists, numbers) and retrieve it later when you need it.

To create a variable, you need to start with a name. You will want to keep your name lowercase and use underscores when you need to make a space. Don’t start your name with a number.

After you create a name, put an equal sign. This signals to the computer that we are assigning a value to it. Then you put your value after it.  Look at the example below.

score = 0
Enter fullscreen mode Exit fullscreen mode

I created a score variable, then assigned (the equal sign) followed by a number. All the setup is on the left side, while everything you want to store is on the right side. When the computer runs this code, any data about the score would be stored in the score variable. 

Variable names are how we identify and access the information. Developers can get this information by just calling its name. It doesn’t matter if it is a string or numbers.

A mistake Coding with Kids students often make when creating variables is using different variable names to store the same information. For example, a student might create a variable called player_direction at the top of their code, while later in a function, they will use a variable called sprite_direction. The computer will display syntax errors since it is unsure which variable to use.

Avoid this mistake by keeping your variables consistent. If you are using player_direction at the top of your code file, use this variable name anytime you need it in your code. You will also need to be careful about naming your variables, too.

A variable name like blue_dog might sound like a good variable name for a player variable, but what if you don’t want the player sprite to be a blue dog? New developers will want to avoid this mistake. In this situation, the developer is better off using the player_one variable name instead of blue_dog for a player variable.

Working with Variables

A lot of programming is about working smarter, not harder, which is why developers are careful with how they name their variables. Variables are great for keeping data organized. Developers use them to store, label, and play with data.

When you create a variable, you can assign new values to it that the computer needs to store. However, you can also update variables in your code. This is where you reassign a variable by updating it with new values and forgetting the old value. Take a look at this variable below.

points = 10
points = 20
print(points)
Enter fullscreen mode Exit fullscreen mode

The first line is where you assign a value to variables. The second line is where we can reassign the value for this variable. The initial value is forgotten while the new one is put in place instead.

Conclusion 

Congratulations! You have accomplished a lot of things today. You created your first variables in Python and did some calculations in Python.

Most importantly, you got some Python code to display on your computer. The next post is about module two. Module two is where students dive deeper into Python.

This means and best practices. Module two also goes over inputs and outputs, as well as exploring more data types.

Top comments (0)