1 of 52

Python🐍 for Poets📚🪶

Introductory Python Сourse for Humanities Researchers

2 of 52

Today:

  • Motivational prologue with live demos 🎥
  • Introduction of teachers and students 👋
  • Starting with Python 👩‍💻 👨‍💻
    • Installation
    • Python as calculator
    • Data types: integer
    • Variables
    • Data types: string
    • Python in Google Colab

3 of 52

4 of 52

Live demo 🎥

5 of 52

So what is the downside?

Linear Warriors, Quadratic Wizards (from TV tropes)

Programmers are a typical example of real-life Wizards 🧙

Excel users are more of a Warrior type ⚔️.

6 of 52

Programming languages vs applications (Excel etc.)

  • Unlike typical desktop, mobile or web ‘Applications’, programming languages usually do not have a graphic interface
  • Because an interface needs limited functionality
  • While programming languages are more or less unlimited
  • They are called languages because their expressive power: you can use them to convey almost any idea to a computer
  • While a desktop application can be compared to a set of prefabricated pictures or signs (e.g. road signs or navy signal flags)

7 of 52

Programming languages vs applications (Excel etc.)

vs

8 of 52

Coding gives you more freedom

  • Freedom to manipulate digital objects like you couldn’t before
    • Apply some change to 100000 files in 100 folders
    • Add up all numbers mentioned in some text(s)
    • Get all the nouns from your corpus and count their frequencies
    • Recognize faces in a movies and detect the share of close-ups (we will not do that)
  • Freedom to create tools for your own needs

9 of 52

End of motivational prologue

10 of 52

Now, round of getting to know each other

11 of 52

Some rules and principles of our course

  • There are no bad questions
  • We are here to make mistakes
  • Programmers learn by making TONS of mistakes and getting frustrated 😂
  • If you’re stuck, raise your hand🙋 and one of us will come to you
  • But since we’re not omnipresent…

12 of 52

General tip: if you’re stuck while programming try these 😉

13 of 52

If everyone is busy doing something and you are lost:

  • Exercises and practical parts are marked like this 👩‍💻👨‍💻
  • All exercise descriptions can be found here in the slides
  • These slides can be found at python4poets.org (and in the moodle)
  • We will post more materials there as the course goes along

14 of 52

And now, Python

15 of 52

Installing Python

  • Go to: python.org/downloads/

16 of 52

Launch the built-in development environment (IDLE)

  • Mac: find IDLE in the Launchpad or in the Applications:

  • Windows: find IDLE in the program search bar

17 of 52

First thing you’re going to see:

This window with a >>> promt is where you can execute python commands in interactive mode. That means that python will automatically print the results of your commands.

18 of 52

👩‍💻👨‍💻 Write this:

>>> print('Hello world')

19 of 52

Congrats! You’ve just written a program! You are already a programmer! Keep on going!

20 of 52

By the way, you’ve just learned to use print()

  • print() is a built-in output function that takes something as input and prints is
  • This is arguably the most used python function
  • You can use it to output results of your code anytime (including in the middle of the program, it won’t break the execution)

21 of 52

Python as calculator;

Integers

22 of 52

You can always use Python as calculator:

23 of 52

+-* work with numbers just as you’d expect them to work:

👩‍💻👨‍💻Try it yourself!

24 of 52

Division is a bit more complicated

  • There are two operators, / and //.
  • The / does the kind of division you’re used to:

25 of 52

Division is a bit more complicated

  • There are two operators, / and //.
  • The // does floor division, i.e. you get only the integer part of the result without the remainder

26 of 52

Variables

27 of 52

Imagine I want to convert my dollars to euros by multiplying them by 1.022946

�Hard to always type it:

28 of 52

Imagine I want to convert my dollars to euros by multiplying them by 1.022946

�Easier to save this number just once under some easy name, like ‘rate’:

29 of 52

Variable

  • Label linked to some data
  • Allows you to call that data by this label when needed

variable

value

assignment

30 of 52

👩‍💻👨‍💻 Exercise:

  • In the first line, assign to a variable some person’s birth year
  • Then use this variable to figure out how old will this person be by end of 2023
  • Print the result (integer number of years)

31 of 52

Strings

32 of 52

String (‘str’ in pythonic)

  • To create a string, you need to surround your text with ‘single’ or “double” quotes
  • Actually, you already did that in your print(‘Hello world’) code
  • Strings can also be added together and multiplied
  • They have other useful features
    • You can compare two strings with ==
    • You can measure string length in characters using len() function
    • You can change the register with .lower() and .upper() methods (more on that later)
    • You can get the exact :

33 of 52

Strings can also be ‘added together’

👩‍💻👨‍💻Try it yourself!

34 of 52

Strings can also be ‘added together’ and even ‘multiplied’

👩‍💻👨‍💻Try it yourself!

35 of 52

Printing many strings, or strings and integers together (the easiest way):

  • Print() function can actually accept several things at once (separated by comma)
  • It will print all of them as one string with a space in between

name = "Jean"

age = 35

bodyweight = 84.3

city = "Marseille"

print(name, "is", age, "years old and lives is in", city, ".")

36 of 52

👩‍💻👨‍💻 Exercise:

  • In the first line, assign to a variable some person’s birth year
  • Then use this variable to figure out how old will this person be by end of 2023
  • 🆕 Print the result in the form of (‘This person will turn N years in 2023’) with correct number in place of N

37 of 52

Useful string methods

.replace() — replaces some things in string

.count() — counts number of occurrences of some string inside a bigger string

38 of 52

Useful string methods

.count() — counts number of occurrences of some string inside a bigger string

39 of 52

More on string methods here

40 of 52

Getting input from user

41 of 52

input() function

  • Built in input function — reads the string entered by the user
  • Inside the brackets this function accepts some text that the user will be shown before s/he can type anything; usually this is an invitation to type in something

42 of 52

👩‍💻👨‍💻 Exercise:

  • 🆕 In the first line, ask the user when is her/his birth year
  • Then use this data to figure out how old will this person be by end of 2023
  • Print the result in the form of (‘You will turn N years in 2023’) with correct number in place of N

43 of 52

Saving files with code

44 of 52

The interactive shell can process only one line (there are exceptions to this rule, but we can ignore them for now)

45 of 52

You can only do it line by line:

46 of 52

Large programs with multiple lines of code are written in a separate file,

not in the interactive shell

47 of 52

How to create in IDLE a file with program:

Cmd + N on Mac:

Ctrl + N on Windows

48 of 52

You’ll see a blank file, like in a simple notepad:

49 of 52

Now type in your code. And that’s already a full-blown program:

50 of 52

👩‍💻👨‍💻 Exercise:

  • Take the previous exercise and save it as a .py file.
  • Run the file by pressing F5 (fn+F5 on mac)

51 of 52

Google Colab

(a more useful, collaborative and friendly

coding environment than IDLE)

52 of 52