Introduction to Programming with Python
By: Tomás Antunes
Tags: Python, Learning, Learning To Code, Programming, Coding

Python is a human-readable programming language that gets interpreted by the computer into machine code. Computations are stored in memory and the CPU does the calculations.

Programs are composed of algorithms which are step-by-step instructions for the computer to perform.

Programming languages have their own ‘syntax’ or rules to describe how statements should be written. You will find that many programming languages share many common features and functions, but each one will have their own syntax rules.

When you are writing code it is very important to maintain readability. Taking a bit of time to choose descriptive words as names for things like variables and functions can really help with this.

Essentially, a program tells the computer how to solve a specific problem. For this we need to read input, detect events, process data and display it on the screen.

To start programming with Python you need a text editor like notepad. There are other editors you can use like Notepad++ or VS Code which will highlight your code with different colors and give you some hints.

You can also use an IDE (Integrated Development Environment) which will allow you to run and debug your program. For Python we have PyCharm, Visual Studio or Thonny.

You might need to install Python by going to the official Python download page.

Let’s start by writing a program that prints a sentence. Copy the following code to your editor.

print("Hello World!")

Then you save the file as helloworld.py, open the command line, change to the directory of your program and run the following command:

python helloworld.py

You should see the sentence printed on the output.

Variables

With variables we can store pieces of data to use on our program. Python features dynamic typing so you don’t need to specify the type of variable. We have the following data types: String, Number, Date, List, Dictionary and Boolean.

In this program we ask the user to write his name and then we greet him. The input() function can be used to read a string from the user. We can concatenate the strings with a plus sign.

name = input("What is your name? ") print("Hello, " + name + "!")

Functions

One of the principles of programming is DRY(Don’t Repeat Yourself). Functions are useful to create blocks of code that we can reuse. With Python we need to pay attention to indentation with tabs or spaces. The keyword for defining a function is ‘def’ and we can pass parameters. To call the function we use parenthesis. Most of the time we want the function to return a value with the ‘return’ keyword.

Here is a program with a function that adds two numbers:

def sum(a, b):

return a + b a = 3

b = 2

c = sum(a, b) print(c)

Conditionals

Python supports the usual logical conditions from mathematics:

Equals: a == b

Not Equals: a != b

Less than: a < b

Less than or equal to: a <= b

Greater than: a > b

Greater than or equal to: a >= b

‘If’ statements allow us to define the logic flow of an algorithm. To group multiple conditions there are the keywords ‘and’ and ‘or’.

In this program we ask the user for a number and determine if it is larger or smaller than 100. Sometimes we have to convert numbers to strings and vice-versa with the functions str() and int().

n = input("Insert number: ") if int(n) > 100:

print(n + " is larger than 100.")

elif int(n) == 100:

print(n + " is equal to 100.")

else:

print(n + " is smaller than 100.")

Loops

With loops we can repeat a block of instructions multiple times. We can loop through a range of numbers or the elements of a list.

Here we have two ways of printing the numbers from 1 to 10:

numbers = [1,2,3,4,5,6,7,8,9,10] for i in numbers:

print(i) for i in range(1,10):

print(i)

FizzBuzz

We can put together what we learned to make this program called FizzBuzz which prints the words “Fizz” and “Buzz” if the number is a multiple of 3 or 5. We use the modulus operator ‘%’ that gets the remainder of a division to determine if a number is a multiple of 3 or 5. With the append() method we can add an element to a list.