Programming for Teens: Beginning Python Tutorial
In this Python tutorial you’ll be introduced to computer programming using one of the most beginner-friendly languages available. By .
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Programming for Teens: Beginning Python Tutorial
30 mins
Variable Types
You encountered variable types a little earlier in this tutorial, but we didn’t cover them in any detail. Each variable type stores a different kind of value.
So far, you’ve only dealt with two basic types in Python: integers and strings. You’ll also encounter boolean types which you can use to store True
and False
values.
Here’s a bit of detail on each of these variable types:
Integers
An integer is a whole number. Integers range from -2147483648 to 2147483647 on 32-bit machines, and from -9223372036854775808 to 9223372036854775807 on 64-bit machines.
You create an integer variable by simply typing the number without any quotation marks, as shown below:
foo = 5
Strings
A string is a sequence of characters; you can use a string to represent anything from text on a screen to entire web requests.
You create a string variable by surrounding the sequence of characters with quotation marks as shown below:
bar = "foo-bar"
Booleans
Booleans represent either True
or False
values.
You create a boolean variable by typing a capitalized True
or False
without quotation marks as shown below:
isFoo = True
There aren’t any quotation marks around the variable; if you did surround True
with quotation marks, you’d be declaring it as a string variable instead!
Adding Strings and Integers
Python makes it really easy to hook strings together, also known as concatenation. You can convert values from integer types to string types using the str()
command. Likewise, you can convert a value from a string to an integer by using int()
.
Enter the following commands into your interpreter:
"1" + "1"
1 + 1
1 + int("1")
Here’s what’s going on in your code above:
- The first statement concatenates the two strings; the quotation marks ensure the numbers are treated as strings. The result is simply
"11"
. - The second statement adds the two numbers together as integers with
2
as the result. - The final statement adds an integer to a string that has been converted to an integer, so it also returns
2
.
If Statements
If statements check whether a conditional is true, and if so, they execute a block of code. Conditionals are usually in the form of value
– operator
– value
and usually compare two values.
For example, you could evaluate whether a variable is equal to zero with the expression x == 0
, where ==
is the equality operator.
Here are some common comparisons in Python:
a == b: #Checks if a and b are equal
a != b: #Checks if a and b are not equal
a > b: #Checks if a is greater than b
a < b: #Checks if a is less than b
a >= b: #Checks if a is greater than or equal to b
a <= b: #Checks if a is less than or equal to b
If statements take the following form:
if conditional:
do_statements_here
Note how the do_statements_here
line is indented. This is how you declare code blocks in Python. It's imperative that every line in a code block is indented by the same number of tabs or spaces as all other lines in the code block; this is a rule enforced by the language itself. In other words, don't mix tabs and spaces!
To create an if statement, type the following into your interpreter:
You'll be greeted by the cryptic prompt ...
; this indicates the interpreter is waiting for your code block.
Press Tab, and type the second line as follows:
Press Enter one more time and your cursor returns to the left side of your console. To add another line to the conditional block, just hit Tab.
If you're done entering lines, press Enter to tell the interpreter that your code block is complete.
Take a look at the example below:
x = 10
y = 10
if x == 10:
print "x equals 10!"
if x == y:
print "x equals y!"
The first if statement checks whether x
is equal to 10; if so, it prints out x equals 10!
. The other if statement checks whether x
and y
; if so, it prints "x equals y!"
.
For Loops
For loops in Python loop through items in a list and set a variable to the current item in the list. A list is a collection of just about anything!
Type in the following code, indenting as shown below:
for x in range(10):
print x
In this example, range(10)
generates a list of numbers from 0 to 9. The for loop, in turn, sets x
to each number in the range.
Just like an if statement, a for loop executes everything indented beneath it. In the above example, the indented code block only contains a single statement.
Since print
is called ten times, once for each item in the list, the program prints the numbers from 0 to 9.
Functions
Functions are reusable blocks of code that perform specific tasks. For example, you could write a function to add two numbers together, or print out a string.
You define and call a function as shown in the example below:
def hello():
print "Hi"
for x in range(3):
hello()
Can you guess what the output of this program is before you run it? Check your answer below:
[spoiler]
It will print out "Hi"
three times, because the for loop will call the function hello
three times.
[/spoiler]
The indented code defines the statements which run when you call the function. Since print "Hi"
is the only indented line under the function declaration, it's the only line that executes when the function is called — not the other lines below.
You call a function by typing the function name followed by open and closed parentheses. As shown earlier, hello()
calls the function you declared above.
Functions are a little like walled gardens: they can't see anything outside of their own little area. This is known as variable scope. If you want a function to work with any outside data, you'll need to pass that data to the function.
This is accomplished using arguments — and no, functions don't argue with each other! :]
An argument is a variable that you pass to a function; the function can then access the value of that variable inside its walls.
You declare a function with arguments as follows:
def add(a, b):
print a + b
The above function defines two arguments, a
and b
, separated by commas between the parentheses. When you call the function, the interpreter sets the value of a
and b
to the value of the variables you pass in.
Take a look at the example below:
def add(a, b):
print a + b
add(1,2) # prints 3!
In the example above, the statement add(1,2)
sets a
and b
to 1 and 2, respectively. The function then prints out the sum of the two numbers you passed in.
The above example prints out the result of the calculation — but what if you wanted to do something with the result of that calculation? What if you wanted to add other values to the result of the function?
To do this, you'll need to add a return statement
to your function.
Consider the code below:
def add(a,b):
return a + b
print add(2, 2) + 1
In the above example, your function adds the two numbers as before, but the return
statement then passes the sum of the two numbers back to the calling statement.
That means the print
statement above takes the value returned by add(2,2)
and adds it to 1, which will give the final value of 5.