Welcome back to Python from Scratch, where we're learning Python...from scratch! In the last lesson, we installed Python and got set up. Today, we're going to cover quite a bit, as we learn the essentials. We'll review variables, operators, and then finish up by learning about control structures to manage the flow of your data.

Video Tutorial

Variables

Variables are the first thing you should learn in any new language. You can think of them as named containers for any kind of data. The syntax to declare them is: name = value You can name anything you like (except for a handful of keywords), and their values can be any type of data.

Data Types

There are many data types, but the following four are the most important:

Numbers

Numbers can be either integers or floating point numbers.

Integers are whole numbers

Floats have a decimal point

Strings

String are lines of text that can contain any characters. They can be declared with single or double quotes.

You have to escape single and double quotes within the string with a backslash. Otherwise, Python will assume that you're using them to end the string. Insert line breaks with \n. Python also supports string interpolation using the percent symbol as follows:

name = "John Doe"
greeting = "My name is %s" % name

You can access sets of characters in strings with slices, which use the square bracket notation:

"Hello"[2] #outputs "l"

Booleans

Booleans represent either a True or False value. It's important to note that you have to make the first letter capital. They represent data that can only be one thing or the other. For example:

isMale = True #Could be used in software with a database of users
isAlive = False #Could be used in a game, set when the character dies

Lists

Lists are used to group other data. They are called Arrays in nearly all other languages. You can create a list with square brackets.

Control Structures

Once you've created and manipulated variables, control structures allow you to control the flow of data. The two types we're learning today are conditionals and loops.

Conditionals

Conditionals allow you to run different blocks of code based on the value of data.

a = 2
b = 3
if a < b:
print "Success"

Loops

The two types of loops we're discussing here are for loops and while loops. for loops work using lists, and while loops work using conditions.

while loops

a, b = 0, 5
while a < b:
print a
a += 1

for Loops

myList = [1, 2, 3, 4, 5]
for a in myList:
print a

Conclusion

That's it for today, but we've covered a bunch of techniques. Feel free to run though everything a few times until it makes sense. I'll try and answer any more questions in the comments, and I hope you'll join me for the rest of the series!