Hello and welcome to part 2 of the programming with Python 3 basics tutorial series. In this tutorial we're going to talk about variables, tuples, strings, loops, and some more basic concepts about programming and Python in general.
To begin, I am going to just make a new Python script to work in (file > new). Now, let's first talk about variables.
Variables are just placeholders for objects, and they let us store these objects to some word or something meaningful to us. For example, we're going to have a variable that stores programming languages, so we're going to call our variable
programming_languages. Variables have to start with either an underscore or a letter, but can also contain numbers, though they cannot have spaces. Part of the beauty of Python is the ease with which we can both write it, and read it. For example, the following is legitimate code:
programming_languages = "Python", "Java", "C++", "C#"
Even for a non-programmer, this is pretty darn legible. It looks like a
list is actually a programming term, and, officially, this actually isn't a list. It's what's called a
tuple. There is also a
list, and they will look fairly similar, but a list will be encased in square brackets (
]). The difference between a list and tuple is that a list is
mutable, a tuple is
immutable. All this means is that lists can be changed. Things added/deleted/modified. Tuples stay as they were initially defined. The main reason you'd want this is just tuples are lighter-weight than lists.
Just in case you forget, or you want to check, you can use
type(), which is a built-in function with Python. This will tell you the type of object that you are dealing with when you pass the object into it. For example:
All together now, we can do something like:
programming_languages = "Python", "Java", "C++", "C#" print(type(programming_languages))
The output of which is:
<class 'tuple'> >>>
Next, we can check out just how simple loops are with python. We can iterate over the tuple, using syntax that's super simple to understand:
for language in programming_languages:
We begin the for loop with the usage of
for then, we give a variable name to each of the items in the thing we're iterating over (in this case, our
tuple), then we use the
in statement, followed by the thing we intend to iterate over (
programming_languages). We end the statement with a colon
: and then a new line. This new line is automatically indented in most editors. White space is what determines a "code block" in Python, as opposed to braces/brackets in many other languages. Now we can begin the block. Let's just
print() out the language. For example:
programming_languages = "Python", "Java", "C++", "C#" for language in programming_languages: print(language)
Python Java C++ C# >>>
I think that just about anyone, programmers or not, can see the above code and expect that output, which is pretty cool! In the next tutorial, we're going to talk some more about lists as well as our project for this series: TicTacToe. Making a simple TicTacToe game will allow us to cover pretty much all of the Python principles that we need, along with how to tackle a project, which breaks down into many smaller programming challenges. See you there!