Date Tags Python

Warning! This article is very much under-construction! The title and everything else is definitely subject to change. I will be editing this article and releasing it live, as I write it.

Lesson 1 - Installing Python, Hello World, and Variables

I am considering renaming this tutorial series to, "Learn Python on Your Lunch Break". I started learning Python back in early 2012 while on my lunch breaks at work and that tradition of studying in short but frequent increments, every single day, is something I have continued since then.

So the idea behind this tutorial series is to introduce complete beginners to programming in short, easily digestible sessions that ideally should not take longer than 25 minutes to consume. I am big fan of the Pomodoro technique and the other study ideals outlined by Barbara Oakley in A Mind for Numbers.

Do Not Copy Paste

So, one thing I have learned over the past few years about learning to code is that you do not want to copy and paste any lines of code when going through a book or tutorial! Type in every single line!

Copy-Paste is your best friend when actually writing code, but when specifically trying to learn to code it can really work against you. Some say it builds muscle memory. I think that is useless and instead it just forces you to slow down and really think about each line of code you write.

Installing Python

Its a fact that most people coming to Python for the first time will have Windows computers and so I have decided to write this tutorial from a Windows perspective. Right out-of-the-box, Windows is not the best development environment, but installing Python does help it exponentially move in the right direction.

I decided to make a quick video on installing Python, so there is no confusion on this critical step:

Hello World!

Ok, so now that you have Python installed, it's time to get this adventure started!

Open your windows terminal by typing cmd into the windows search bar. Then, bring up Python's interactive interpreter by typing:

> python

Below is a quick gif to show you how to bring up the command prompt in Windows 10, if you have never done it before.


The Interactive Interpreter

Python's Interactive Interpreter gives you a command line interface where you can type Python code in, and see the results of that code almost instantly. The Interactive Interpreter is great for testing snippets of code out and even can be a great way to inspect Python objects to find out more information about their attributes and methods.

Inside the Interactive Interpreter, go ahead and type the following command and you should see the output below:

>>> print("Hello World!")
Hello World!

Saving your code in a file

The Interactive Interpreter is great but it doesn't give you a lot of easy ways to save the code you are typing in. For that, we will need a basic text editor. Luckily for us, Python comes with a simple text editor built right in called IDLE!

I created a very quick video to show you how to launch and write you first Python script using IDLE:

Basic Data Types in Python

So, before we talk more about the print() function in Python we need to understand three basic data types that exist in programming languages and specifically, Python.

  1. strings - a string is simply a sequence of characters. For example, "Hello World" is a string literal in Python. A string literal just means that this string is hardcode into the text file and is unchangeable.
  2. integers - you can think of an integer as a whole number. It is a number with no fractional part. For example, -1, 1, 2, 3 are all integers.
  3. floats - You can think of a float as a number with a fractional part. In school, you might have called them decimals. For example, 1.234, 3.145, .0893 are all floats.

Keep in mind that I am simplifying some things here, but these are the essential primitive data types in Python. Later we will learn about some of the other basic data types like lists, dictionaries, tuples, and more!


Now let's talk variables!

What is a variable? It is the opposite of a literal! That's a bit snarky but it really is true. "Hello World!" is a string literal in Python but we can assign that value to a string variable and name that variable whatever we want. Let's try it!

# "Hello World" is a string literal
# my_string is a string variable
my_string = "Hello World!"

# 100 is an integer literal
# my_integer is an integer variable
my_integer = -100

# is a float literal
# my_float is a float variable
my_float = 3.1415

# By the way, I am a comment and Python ignores me :(

This process of assigning some type of value to a variable is called assignment.

What about declaring the variables?

I will mention this because it confuses some people who have some basic experience with other programming languages like C. C and many other programming language are statically typed. This means that the type of the variables is known at compile time. From a practical standpoint, this means the programmer must identify the type of the variable.

For example, declaring and assigning an integer in C, would look like this:

// This is C code, not Python
int my_int = 100;

Python is dynamically typed, and from a practical standpoint this means that we do not identify the types of our variables at runtime. You still have to be careful though as if you perform the wrong operations on a data type your program can error out or even worse continue but product unexpected results.

Printing our Variables

Now, that we know how to assign basic variables, let's take the print() function we saw earlier and print our variables to the terminal.

# save this file as and then run it from IDLE
# or from the command line by typing:
# python 

my_string = "Hello World!"
my_other_string = 'Hello other World!' # You can create strings with either `` or ""
my_integer = -100
my_float = 3.1415


When you run this, you should get the following output:

> python
Hello World!

That's it for now

Ok, so that is it for now. In the next article we are going to go lists and get an introduction to basics concepts like using functions.

As usual, feel free to comment below or contact me if you come across any errors in this post!


comments powered by Disqus