Python basics 1 - Intro to Python

Overview

Teaching: 30 min
Exercises: 0 min
Questions
  • What is Python?

  • What are variables?

Objectives
  • Assign values to variables.

  • Select individual values and subsections from data.

  • Convert datatypes

What is Python?

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python’s simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. – python.org

Why learning coding as a non-CS student?

(you can watch it if you have time)
IMAGE ALT TEXT

Variables

Lets start by learning about variables. A variable is a name for a value, you can name it whatever you want. Python’s variables must begin with a letter and are case sensitive. We can create a new variable by assigning a value to it using =. When we are finished typing and press Shift+Enter, the notebook runs our command.

This is how you assign a value to a variable:

a = 1

Ok, now you have successfully assigned value 1 to a variable called a.
To see the value in the variable, we can simply do:

a

or

print(a)

Datatypes

Datatype is to classify the value of variables. In the previous example, you assigned a numerical value to a variable. There are a lot of built in datatypes, you can check here for details.
Lets briefly review some common built in datatypes.

1. Boolean

boolean has only two possible values: True or False.

2. Numerical

3. Strings

3. Sequence types

4. Dictionary

checking datatypes

To check the datatype of a variable, we can use type() function:

type(dt)

changing datatypes

You actually have done this previously. You have successfully converted a list to a set. To change datatypes, we can do desired_datatype(variable). For example, we can convert the dictionary dt to string by:

str(dy)

You can convert a float to an integer by:

int(1.7)

Note that, int() always rounds down, so you will get 1 if you do int(1.7)

However, this would not always work. Some datatypes are not interchangeable. For example, you cannot convert a dictionary to a integer by int(dt). It is kind of intuitive, an integer is a single number, but a dictionary is {key : value} pairs.

Arithmetics

You can get output from python by typing math into the console:

3 + 5
2 * 7

and do arithmetic with variables:

volume = 750 * 6
print('volume in litter:', volume/1000)

As the example above shows, we can print several things at once by separating them with commas.

We can also change a variable’s value by assigning it a new one:

volume = 250 * 6
print('volume in litter now:', volume/1000)

Key Points

  • Use variable = value to assign a value to a variable in order to record it in memory.

  • Variables are created on demand whenever a value is assigned to them.

  • Use print(something) to display the value of something.