Your first Python program!

Your first Python program!#

As we mentioned in a previous lesson, Python is an interpreted language. This means the interpreter executes instructions without requiring them to be compiled into machine language beforehand.

In Python, the interpreter enables what’s known as an interactive REPL (Read-Eval-Print Loop), or shell, which reads a piece of code, evaluates it, and then prints the result to the console in a loop. The Jupyter notebooks we will be using throughout the course implement an enhanced version of this shell, known as IPython (https://ipython.org).

Note

You can also use Python as a scripting language. In this case, the interpreter runs source files as executable programs by loading the file and executing the code line by line. Interactive mode, in contrast, refers to launching the interpreter and using it as a platform to run code that you type in directly.

What kind of instructions can we ask Python to do? For example, Math!! Python is great calculator :)

# Addition
1 + 1
2
# Substraction
2 - 5
-3
# Multiplication
10*10
100
# Division
1/4
0.25

+, -, *, / are common Arithmetic operators. And you might be wondering… what is an operator? We will get to this in a next lesson…

For now, let’s create our first Python program!!!

print("Hello, DS-1002 class!")
Hello, DS-1002 class!

This is an adaptation of the so-called Hello World program, which simply prints “Hello world”. Here, this is done through the print function. A function, like in mathematics, is just an entity that takes some input (aka “arguments”) and, through certain computations, yields an output.

In Python a function has the form function_name(arg1, arg2, arg3...), that is, a name followed by parenthesis that contain certain arguments (i.e., inputs). When you run it, the function takes the inputs, executes some piece of internatally and yields (or not) an output.

Here, the print function takes anything we writen down between the parentheses and prints it to our screens. Did you seen how easy this is in Python?

This also gives you a first look into functions in Python. We will come back to this soon!

# We print as many messages as we want when we execute a cell:
print("Hello, DS-1002 class!")
print("My name is Javier, and I am going to be your instructor this semester")
Hello, DS-1002 class!
My name is Javier, and I am going to be your instructor this semester

Exercise 2

Write a piece of code that, when run, greets you by printing “Greetings!” on the screen. Then it prints a message on the screen asking your name. You reply to this message saying what is your name. Finally, the program greets you by printing “Greeting,” followed by your name name.