Getting Started with Python — A Tutorial for Beginners (Part 1)
If you’re like me you’ve dabbled in Python here and there. Curious to see what the hype is all about maybe you even downloaded an IDE and played around with some sample code but got stuck, gave up, and never clicked that icon again. As a source of self-motivation to dust off some half-completed personal projects and to share my experience — hopefully inspiring someone else who just needs that extra push — I’ll be writing a short series on getting started with Python. Automate the Boring Stuff with Python will be referenced as source material for this series and is available for free under a creative commons license. For the more financially well-to-do, you can pick up a copy wherever you like to purchase your books. Now let’s dig into some code!
Getting Our Bearings
Let’s identify a few fields in our environment so that we have a common parlance with which to refer to things. I am using Thonny IDE. It is a less popular version of Python but it is very beginner friendly. Also, it came with my Raspberry Pi that I’m using as a first-timer’s Linux machine (pray for me) and it’s decent enough for what we’re doing here. Most IDE’s will have a similar layout so pick your poison.
The editor is where our code will actually live, but for quick testing and other actions like quick variable assignment we can use the shell. Let’s try it! In the shell, type in 2 + 2 and hit enter. If you did it right, it should look something like this:
Sweet! We just ran some code. Don’t sprain your wrist patting yourself on the back though, because that little beauty we just wrote isn’t stored anywhere and we’ll lose our hair about as fast as we lose our work if we stick to the shell.
Finding Stability in Variables
Not only can we more easily manage our precious code by using the editor instead of the shell, but we can also save those values we’re using in things called variables. Think of variables as little bins that you can store things in: integers, strings, and a lot more! It would get complicated (sometimes downright impossible) really quickly if every time we wanted to write a program we had to hard code everything. We use variables to store the results of these expressions as well as give us a quick way to reference, incorporate, and even change these values later on in our program. Let’s make a variable now! In your editor, type the following:
1 | myVariable = 2 + 2
If you press run here, you’ll notice it looks like your program does something but you don’t see a result. Depending on your IDE, you may get a screen that shows a list of variables you have assigned. In that case, you will see myVariable there. However, we can force our program to show us a result with the print() function! We can print variables whether they contain integers, strings, and other things. Let’s try:
1 | myVariable = 2 + 2
2 | print(myVariable)
3 | secretPassword = ‘iHeartPython’
4 | print(‘I did it!’)
Let’s go over some things we just learned:
- Variables are assigned with the ‘=’ sign
- We can even store strings in variables using the apostrophe sign to enclose the word(s)
- print() shows us the value of that variable
Feel like you’re getting somewhere? Good! Let’s close out this chapter with a short and fun little program incorporating what we just learned as well as adding a few new functions to the mix.
What’s the Secret Password?
We’re going to make use of a couple of new functions in this section.
- The input() function prompts the user to type something and press enter
- The int() function makes a value an integer
- The str() function makes a value a string
So let’s see if we can use this to have a “conversation” with our computer!
1 | myName = input(‘What is your name? ‘)
2 | print(‘Hello, ‘ + myName + ‘! My name is HAL.’)
3 | myAge = input(‘How old are you? ‘)
4 | print(‘Cool! So your name is’ + myName + ‘ and you are ‘+ myAge + ‘ years old.’)
5 | newAge = int(myAge) + 5
6 | print(myName + ‘, did you know in 5 years you will be ‘+ str(newAge) + ‘?’)
Press run and follow the prompts. Pretty cool, right? A couple of syntactical notes:
- In line 3 myAge was assigned as part of a prompt which made it a string by default. This is why in line 5 we had to use the int() function to reassign it as an integer so we could successfully add 5 to myAge.
- When we want to string together multiple values (sorry, couldn’t help myself) as one big string, our syntax looks like this:
print(‘stuff I want to say ‘ + aVariable_i_assigned + ‘ some other very important and interesting things’)
Note the use of apostrophes, the use of spaces to make it more readable, the plus signs, and the variables used without apostrophes. Apostrophes are our way of saying, “hey, within these brackets I want this to be recognized as a string”. How could we mess this up? If we said:
We would get:
in the shell instead of — well whatever you put in as your age. Don’t worry, this is a judgement-free zone.
Have some fun with it, try some new things, see if you can break the code and see what causes errors. Remember, it’s expected and encouraged to make mistakes! In part 2 we’ll take a look at conditional statements and have our program start making decisions.
If you’re stuck and would like to access the GitHub repository for this series click here.