Overview

Content

Variables and Data

You can assign different types of informations to variables. Variable names cannot contain whitespace and cannot start with a number. These types of informations are datay types. A few basic ones in Python are integer numbers, floating point numbers, boolean types and strings.

my_age = 28                      # int
price_for_one_item = 0.5         # float
my_name_is_philipp = True        # bool
my_name_is_peter = False         # bool
my_name = "Philipp von Neumann"  # str
my_last_name = 'Neumann'         # str
 
print(my_name)
print(my_age)

Artihmetic

In Python you can just use the standard arithmetic operators and ignore whether the numbers are floats and integers. A division returns always a floating point number.

my_age_in_years = 35
my_age_in_months = my_age_in_years * 12 # multiplication
print(my_age_in_months)
 
time_in_ms = 2000
time_in_seconds = time_in_ms / 1000 # division
print(time_in_seconds)              # result is always a floating point number
 
my_favorite_number = 4
my_new_favorite_number = 1 + my_favorite_number # addition
print(my_new_favorite_number)
 
number_of_hairs_on_my_head = 1298348
number_of_hairs_on_my_head_after_one_week = number_of_hairs_on_my_head - 1238 # subtraction
print(number_of_hairs_on_my_head_after_one_week)
 
var1 = 13
var2 = 3
var4 = var1 // var2 # integer division
print(var4)         # result is always a integer number

You can also use this kind of shorthand notation for your operations:

var1 = 35
var1 *= 12   # multiplication
print(var1)
 
var2 = 2000
var2 /= 1000 # division
print(var2)  # result is always a floating point number
 
var3 = 4
var3 += 1    # addition
print(var3)
 
var4 = 1298348
var4 -= 1238   # subtraction
print(var4)
 
var5 = 13
var5 //= 3   # integer division
print(var5)  # result is always a integer number

Control Structures and Logic

Boolean and If-Statements

With comparison operators you can check if a statement evaluates to true or false. This is usefull when you want to execute parts of your program depending on this evaluation.

# == (Equal)
# < (Less than)
# > (Greater than)
# != (Not equal)
# <= (Less or equal than)
# >= (Greater or equal than)
 
a = 5
b = 10
 
test1 = (a != b)  # evaluates to true or false
print(test1)
 
test2 = (a == b)
print(test2)
 
test3 = (a < b)
print(test3)
 
test4 = (a > b)
print(test4)
 
test5 = (a <= b)
print(test5)
 
test6 = (a >= b)
print(test6)
 
# if-else statement
if test1:
    print("a is not equal b")
else:
    print("a is equal b")
 
# if-else-if statement
if test2:
    print("a is equal b")
elif test3:
    print("a is less than b")
elif test4:
    print("a is greater than b")
elif test5:
    print("a is less or equal than b")
elif test6:
    print("a greater or equal than b")
else:
    print("What are testing at all?")

Writing and Reading text files

With Python you can read and write normal text files and binary files with built-in functions.

With:

file_object = open()

(Virtual) Environments

Python projects exist in virtual environments, short for “venv”. A venv helps to define a list of libraries which are necessary for a specific project. Normally with Python you don’t install libraries globally, because projects can rely on specific versions of a library. A good practice is to exclude your venv dir from version controlling.

Creating a venv

  • You can create a venve withthe terminal command python3 -m venv .my-venv
  • after this you can activated it with source .my-venv/bin/activate
  • and deactivate with deactivate

Requirements

A requirements.txt is file with a list of Python libraries, that are necessary for a specific Python project.

  • you can ‘freeze’ your dependencies of the project in a text file via pip3 freeze > requirements.txt
  • after this someone can install the dependencies with pip3 install -r requirements.txt

Python in Emacs

Configurations

I use Python mostly in Emacs. For this you need to configure your Emacs properly. I use the LSP-Mode and some other packages. A few will follow.

Commands

  • start your Python interpreter with C-c C-p
  • run a line from a file in interpreter with C-c C-e
  • run your file in interpreter with C-c C-c

Anaconda

Anaconda is a distribution of Python and R. It allows easy installation of a Python interpreter + various data science packages.