Skip to content
Snippets Groups Projects
Commit f9368dd3 authored by Máté Zoltán Farkas's avatar Máté Zoltán Farkas
Browse files

Minor update

parent 8feceac3
Branches
No related tags found
No related merge requests found
%% Cell type:markdown id:897fcfcd tags:
# Python Tutorial
-----------------------
Welcome to this warm-up tutorial! Let's take a look at some highlights of Python!
Let's start with the usual **hello world** program:
%% Cell type:code id:c8562e26 tags:
``` python
print("Hello World!")
```
%% Output
Hello World!
%% Cell type:markdown id:48a13786 tags:
You can print multiple items in a row:
%% Cell type:code id:bea4da9f tags:
``` python
print("1 + 12 =", 13)
```
%% Output
1 + 12 = 13
%% Cell type:markdown id:5a70d15c tags:
## Comments
You can also comment your code with ```#``` for a **single line** or with ```"""comment"""``` for **multiple lines**:
%% Cell type:code id:6b5d414b tags:
``` python
# This is a comment and will be ignored upon execution
```
%% Cell type:code id:d69d90e9 tags:
``` python
print("What's that thing that spells the same backwards as forwards?")
"""
I am a comment too!
See, this line is ignored as well!
"""
print("A palindrome...?")
```
%% Output
What's that thing that spells the same backwards as forwards?
A palindrome...?
%% Cell type:markdown id:c4c8535a tags:
## Variables and Data-Types
Python is a **dynamically-typed** language, i.e. you don't have to define your variable types:
%% Cell type:code id:f7179786 tags:
``` python
a = 13 # This is an integer
b = "This is a string" # This is a string
c = True # A boolean
d = 14.5 # This is a double-precision floating-point variable (called a 'double' in C-speak)
e = [1, 3, 15, 34] # This is a list, an array of objects
f = ("Emmental", "Brie", "Gouda", "Danish Blue") # This is a tuple, an immutable array
```
%% Cell type:code id:d0586d1b tags:
``` python
# You can use squared brackets to access elements in an array or tuple
# What's the second element in array e?
print("The second element in a is:", None,"(should be 3)")
```
%% Output
The second element in a is: None (should be 3)
%% Cell type:markdown id:0019db03 tags:
## Elementary Arithmetics
You can use the usual **operators** +, -, \, / for elementary arithmetics:
%% Cell type:code id:86b8e68c tags:
``` python
# Use the variables a and d defined above to calculate 13 x 14.5!
print("13 x 14.5 =", "?")
```
%% Output
13 x 14.5 = ?
%% Cell type:code id:29eac6ed tags:
``` python
# Booleans work as numbers as well. What happens if you add a and c?
print("13 + True =", "?")
```
%% Output
13 + True = ?
%% Cell type:markdown id:f6d98d65 tags:
## Comparison Opeartors
You can use <, >, ==, <=, >= to **compare numbers** (**and objects**), the **keywords** ```and```, ```or``` to compare statements (booleans) and the keyword ```not``` to negate them.
%% Cell type:code id:b0a7cbe1 tags:
``` python
# What's the output of ("a bigger than d" AND "c is True")?
print((a==14) and not (c<d))
```
%% Output
False
%% Cell type:markdown id:cbba9080 tags:
## Control Flow Tools
For **conditional branching**, the usual keywords ```if```, ```then``` and ```else``` are defined.
%% Cell type:code id:784cb3e8 tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment