64x64 icon dark hosting
Choose a hosting plan here and get a free year's subscription to Tuts+ (worth $180).
Advertisement

Python from Scratch: Variables, Data Types and Control Structure

by
Gift

Start a hosting plan from $3.92/mo and get a free year on Tuts+ (normally $180)

This post is part of a series called Python from Scratch.
Python from Scratch: Getting Started
Python from Scratch - Functions and Modules

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:

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

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:

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.

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:

If you nest a list within another list, you can access them with multiple indexes.


Comments

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.


Operators

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.

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


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.

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

for Loops


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!

Advertisement