Week 10 – Introduction to Python Programming

Lesson Overview

Topic: Introduction to Python Programming

Activities & Due Dates[Activity 6] Hello World in Python

Opening – Rethinking: what is the computer?

a simple machine that receives inputs, processes, and shows outcomes.

What is computer?

Content 1 – Evaluation of computer languages

Evaluation of computer languages

Content 2 – A brief history of programming language

  • Some influential ones:
    • FORTRAN
      • science/engineering
    • COBOL
      • business data
    • LISP
      • logic and AI
    • BASIC
      • a simple language
Development of programming languages

Activity 1 – Setting up programming environment in Raspberry Pi

1. Move to MyProgramms folder

Create a new folder

2. Check Hello.c file and close

Program Hello world

3. Generate Assembly file and compare with Hello.c

Compile Hello.c

Content 3 – What is Python?

  • Created by Guido van Rossum in 1991
  • A programming language
    • interpreted
    • high-level
    • general-purpose – basic computing, game, business, graphics, and all.
  • Python features
    • 1. Easy to learn and use
    • 2. Expressive language (understandable and readable)
    • 3. Interpreted language (execute line by line – easy to debug)
    • 4. Cross-platform
    • 5. Free and open source
    • 6. Object-oriented
    • 7. Extensible (Easily integrated with languages like C, Java, and C++)
    • 8. Large Standard Library
    • 9. GUI Programming Support

Content 4 – Programming basics

•code or source code: The sequence of instructions in a program.

•syntax: The set of legal structures and commands that can be used in a particular programming language.

•output: The messages printed to the user by a program.

•console: The text box onto which output is printed.

•Some source code editors pop up the console as an external window, and others contain their own console window.

Console program output

Content 5 – Compiling and interpreting

•Many languages require you to compile (translate) your program into a form that the machine understands.

Compiling C

•Python is instead directly interpreted into machine instructions.

Interpreting Python

Activity 2 – Running Python on Raspberry Pi

•Thonny Python IDE

Python IDE in Raspberry Pi

Activity 3 – Python Expressions

•expression: A data value or set of operations to compute a value.

  Examples:  1 + 4 * 3

•Arithmetic operators we will use:

+ – * /    addition, subtraction/negation, multiplication, division

%   modulus, a.k.a. remainder

**    exponentiation

•precedence: Order in which operations are computed.

* / % ** have a higher precedence than + –

1 + 3 * 4 is 13

•Parentheses can be used to force a certain order of evaluation.

(1 + 3) * 4 is 16

Activity 4 – Real numbers in Python

•Python can also manipulate real numbers.

Examples: 6.022  -15.9997  42.0  2.143e17

•The operators + – * / %  ) all work for real numbers.

•The / produces an exact answer: 15.0 / 2.0 is 7.5

•The same rules of precedence also apply to real numbers:
Evaluate  ( )  before  * / %  before  + –

•When integers and reals are mixed, the result is a real number.

Example:  1 / 2.0  is  0.5

The conversion occurs on a per-operator basis.

Conversion rules

Activity 5 – Variables

  • variable: A named piece of memory that can store a value.
  • Usage:
    • Compute an expression’s result,
    • store that result into a variable,
    • and use that variable later in the program.
  • assignment statement: Stores a value into a variable.
  • Syntax:

  name = value

Examples:  x = 5

  gpa = 3.14

Variables

•A variable that has been given a value can be used in expressions.

  x + 4 is  9

Activity 6 – Print out a message in Python

•print : Produces text output on the console.

•Syntax:

  print (“Message“)

  print (Expression)

•Prints the given text message or expression value on the console, and moves the cursor down to the next line.

  print (Item1, Item2, , ItemN)

•Prints several messages and/or expressions on the same line.

•Examples:

  print (“Hello, world!“)

  age = 45

  print (“You have”, 65 – age, “years until retirement“)

Output:

  Hello, world!

  You have 20 years until retirement

Activity 7 – Data types

•Numeric

numeric data types

•Integer

•Float

•Strings

string data type

•And others: Boolean, Date, List …

Activity 8 – input

•input : Reads a number from user input.

•You can assign (store) the result of input into a variable.

•Example:

  age = input(“How old are you? “)

  print (“Your age is”, age)

  print (“You have”, 65 – int(age), “years until retirement“)

  Output:

  How old are you? 53

  Your age is 53

  You have 12 years until retirement

•Exercise: Write a Python program that prompts the user for his/her amount of money, then reports how many Nintendo Switches the person can afford, and how much more money he/she will need to afford an additional Switches. (Assume a Nintendo Switch is $299)

•hint: use / and % operators

•Convert input value using int(input), because the input value is a text variable.