tag: beginner | Python Tutorial

Tag: beginner

Read CSV

A CSV (Comma Separated Values) file is a file with values seperated by a comma. Its often used to import and export with databases and spreadsheets.

Values are mostly seperated by comma. Sometimes another character is used like a semicolon, the seperation character is called a delimiter.

Related Course:
Data Analysis with Pandas and Python

Read CSV with Pandas

Pandas is a data analysis library. If you work with data a lot, using pandas is way better.
Lets say you have a csv file containing nation statistics:

Country, Capital, Language, Currency
United States, Washington, English, US dollar
Canada, Ottawa, English and French, Canadian dollar
Germany, Berlin, German, Euro

We can read a csv with the lines:

import pandas as pd
import numpy as np

df = pd.read_csv('nations.csv')

for country in df['Country']:

Pandas works with dataframes which hold all data. Thats why we can use the rows like df[‘name’].

Read CSV

If you don’t want to use pandas, you can use the csv module to read csv files.
This is as simple as:

import csv

with open('nations.csv') as csvfile:
csvReader = csv.reader(csvfile, delimiter=',')
for row in csvReader:

We can access individual cells like so:

for row in csvReader:

Getting started

To get started, you will need the Python interpreter or a Python IDE. An IDE is a tool that will make the experience of software development much better.

Related course:

Python interpreter

Python programs are simply a collection of text files. If you want something more sophisticated than notepad for editing, you will need a Python IDEs (recommend). A Python IDE will make programming Python easier.

You can download Python interpreter here: https://www.python.org/downloads/

The Python interpreter is a command line program, we’ll discuss it in the next lecture.

Python IDE

An IDE generally supports listing all program files, syntax highlighting and other features. There are lots of Python IDEs you could choose from.

Using one of these Python IDEs makes programming easier than in say, notepad. It will automatically color the text like the example below:

pycharm python IDE

How to run

Learn how to run Python code?

You can execute Python code from the terminal or from a Python IDE. An IDE is a graphical environment that assitsts in software development.

If you are new to Python, I recommend this course:

How to run Python

All Python programs are written in code, text files with lots of instructions. These are saved with the extension .py.

A program made in Python can be one or more .py files.
python program

To run a Python program, you need to have Python installed. Start the terminal.
In the terminal, type the command below to test if Python is installed

python --version

Python installed?

Open a terminal and enter the directory of your program. The directory may contain more than one python (.py) file.

cd your_app_directory

In the terminal type:

python file.py

where file.py is the name of your program. If you have more than one file, the main program is often the name of the program itself;

Use an IDE

Sometimes an easier way to execute Python programs is using a Python IDE. What is an IDE?

An IDE is a tool that assists in software development. Its a software program that has many features like code coloring (syntax highlighting), file navigation, quick search and much more.

In most IDEs, there’s a play button that instantly runs the Python program. In other words, in a Python IDE, you simply press the ‘play’ button.

pycharm toolbar

The image above shows the header of the program PyCharm, a Python IDE. The green play button can be used to start a program.

Download exercises

Text Input

Want to get keyboard input?

To get keyboard input, use the input function. Inside the terminal you’ll be able to type text.

In Python 3 you can use the input() function, older versions of Python have the raw_input() function.

Related Courses:

User Input

The input function has a return variable. In this case the keyboard input. If we do not store the return variable into a programs variable, we lose it. That’s why we write a variable to store the result in.

To get a text value:

name = input("Enter a name: ")

This will show you:

Enter a name:

You can now give keyboard input, it will be stored in the variable name.

keyboard input

Note: don’t forget to store the return variable. variable = input(“..”)

Numeric input

To get a whole number:

x = int(input("What is x? "))

To get a decimal number:
x = float(input("Write a number"))

Download exercises

Text output

Text output is one of the basics in Python programming. Not all Programs have graphical user interfaces, text screens often suffice.

You can output to the terminal with print function. This function displays text on your screen, it won’t print.

Related Courses:

The terminal is a very simple interface for Python programs. While not as shiny as a GUI or web app, it’s good enough to cover the basics in.

Create a new program (text file) in your IDE or code editor.
Name the file hello.py. It only needs one line of code.

To output text to the screen you will need this line::

print("Hello World")

Run the program (from terminal: python hello.py)
If you run the program:

Hello World

The program above prints everything on a single line. At some point you’ll want to write multiple lines.

To write multiple lines, add the ‘\n’ character:

print("Hello World\nThis is a message")

Results in:

python print function newline
Note: the characters \n create a new line

Download exercises

To print variables:

x = 3

This will show:


To print multiple variables on one line:

x = 2
y = 3
print("x = {}, y = {}".format(x,y))

Will give you:

x = 2, y = 3

Download exercises