Introduction to Python

einbahnstrasse.github.io/MTEC1003-slides/slides/python.intro.tutorial.v01/

MTEC1003 — Media Computation Skills Lab

In this tutorial, we’ll discuss…

Values

Types

Operations

Variables

Functions

and Input/Output

…but in Python, NOT JavaScript!

Similar to JavaScript

Python, like JavaScript, relies on simple functions and variables without engaging in class definitions.

But, unlike JavaScript

Python supports writing much larger programs…

There are many differences

but in MTEC1003, we’ll focus on the

fundamental similarities

between these two languages.

Not for the browser

  • We won’t be writing Python in web pages.
  • But we will write Python in a text editor…
  • to be executed on the command line.

unlike our use of JS

Our Tools

  • your text editor to write/edit Python scripts
  • the command line to execute and debug
  • Git for version control

compare to JS

Values

  • numbers, e.g. 1, -2, 3.14, 35e3
  • strings, e.g. “Hi Fred!”
  • boolean, e.g. True, False

c.f. Values in JS

Numeric Operators

+

-

x

÷

%

c.f. Operators in JS

Remember your PEMDAS

(Parens)

Exponents (powers, roots)

Multiply

Divide

Add

Subtract
more on PEMDAS

Comparison Operators

<

<=

>

>=

==

!=

is

is not
more on Python operators

Strings


  "Hi, I'm a string."
  'also a string'
  "string containing numbers: 4, 5.7"
  "string ending with a new line\n"
  "" # <= empty string
  '' # <= empty string w/single quotes

c.f. Strings in JS

String Concatenation


  "hello" + "there"
  'you have ' + str(450000) + ' unread emails!!'

c.f. String Operators in JS

Wait, what’s going on in that last line?

We used str() to turn 450000 into a string.
Then we concatenated it into a larger string:
‘you have 450000 unread emails!!’
I mean, WOW.

Statements in Python


  1 + 3
  4 x 5

Notice there is no semicolon at the end of a line in Python! And how do we do Statements in JS?…

Statements in JavaScript


  1 + 3;
  4 x 5;

Notice anything different?

Friendly reminder:

All statements end in a semicolon in JavaScript!

Any Questions?

Variables

Remember in Javascript how we declare variables?


  var x = "someValue";

Well, it turns out variables,
in all languages,
must be declared in some way…

Variables in Python

This couldn’t be easier…


  x = 'someValue'

So, what’s different?

We don’t need to type var to declare a variable.

In Python, a variable is declared
as soon as it’s assigned a value!

Function

  • a sequence of statements performing some action
  • ordered statements: like checking off a to-do list
  • can take inputs & return values, but doesn’t have to

Calling functions

To execute (i.e. to “call”) a function,
type its name followed by parens…

A Built-in Python Function


  print("you have 450000 unread emails!!")

print() is the funcion call.
Input to the function goes inside the parens.
So, the string is its input.
c.f. Functions in JS

A Totally Stupid Program


  answer = input("Say something! ")
  print(answer)

1. Declare variable: answer
2. Prompt user with input()
3. Echo it with print()
c.f. same thing in JS

Finé

Now, time to do the labs…