A basic introduction to Python, part I

Introduction

In this unit, we will make a quick introduction to the basics of Python, with the aim of getting you up and running, ready to start some more stats and probability oriented labs.

Basic facts

Python programs

Thanks to Visual Studio Code, we will write small Python programs, which are plain text files with extension .py. 

These files, when executed from Visual Studio Code or from the command line with python myfile.py, are run sequentially from the first line to the last line.

First characteristics of Python

Data structures

Python admits many data structures which allow a powerful and flexible manipulation of objects. We will only see a few basic ones in the following videos.



  A tour of the basic data structures we may use in the course

Integers, floats, booleans and strings

Lists

Tuples

Sets

Dictionaries

  Match the piece of code with the data structure it defines

Matching activity with 4 pairs

  Classify as true or false

  • In Python, I can begin any line with any number of white spaces or indents

  • Tuples must contains items of the same type

  • Sets is a collection of non duplicate items

  • Lists are inmutable, that is I cannot change the value of an item in a list

  • In Python, I must declare a variable indicating its type before I can use it in my program

  • To surround a string in Python I can use single, double or triple quotation marks

Basic input and output

  Click on the image

Basic input and output

Loops

  Click on the image

Loops in Python

  In a Python file called challenges_basic.py, solve the following challenges

  Click on the image

In the previous program, add a block of code that requests an integer n from the user and prints the multiplication table of n.

  Click on the image

Conditional statements

As in any programming language, the conditional statements are a basic building block of a program. They allow to trigger a series of instructions when a given condition holds.

Condit

Challenge 4:

Add to your program a block of code that requests an integer n from the user and checks whether n is a prime number