Python from Scratch: Variables, Data Types and Control Structure
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.
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.
There are many data types, but the following four are the most important:
Numbers can be either integers or floating point numbers.
- Integers are whole numbers
- Floats have a decimal point
String are lines of text that can contain any characters. They can be declared with single or double quotes.
empty = "" escaped = "Can\'t" greeting = "Hello World" multiLine = "This is a long \n\ string of text"
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" #outputs "l"
Booleans represent either a
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 are used to group other data. They are called
Arrays in nearly all other languages. You can create a list with square brackets.
emptyList =  numbersList = [1, 2, 3] stringsList = ["spam", "eggs"] mixedList = ["Hello", [1, 2, 3], False]
As you can see above, lists may contain any datatypes, including other lists or nothing at all.
You can access parts of lists just like strings with list indexes. The syntax is the same:
numbersList #outputs 2 stringList #outputs spam mixedList #outputs 3
If you nest a list within another list, you can access them with multiple indexes.
Comments are used to describe your code, in the case that you want to come back to it later, or work in a project with someone else.
#This a comment on it's own line #You create them with the hash symbol var = "Hello" #They can be on the same line as code
You've seen operators before. They're those things like plus and minus, and you use them in the same way that you learned in school.
2 + 3 #Addition, returns 5 8 - 5 #Subtraction, returns 3 2 * 6 #Multiplication, returns 12 12 / 3 #Division, returns 4 7 % 3 #Modulo, returns the remainder from a division, 1 in this case. 3**2 #Raise to the power, returns 9
You can also assign the result of an operation on a variable back to the same variable by combining the operator with an equals sign. For example,
a += b is a more concise version of
a = a + b
x = 2 x += 4 #Adds 4 to x, it now equals 6 x /= 2 #Divides x by 2, it now equals 3
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 allow you to run different blocks of code based on the value of data.
a = 2 b = 3 if a < b: print "Success"
The two types of loops we're discussing here are
for loops and
for loops work using lists, and
while loops work using conditions.
a, b = 0, 5 while a < b: print a a += 1
myList = [1, 2, 3, 4, 5] for a in myList: print a
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!