Introduction to Computing and Python

Eric Brauer

Introduction

As discussed, we are introducing the Fundamentals of Programming, including:

  • Logic
  • Syntax
  • Algorithms

Terminology

Logic
Getting a computer to do what you need requires us to use certain logical structures, such as checking conditions, looping, etc.

These patterns are common to most (all?) programming languages.

Terminology

Syntax
The keywords and punctuation that are used in a particular programming language. We will be learning Python syntax.

Terminology

Algorithms
Breaking down a bigger problem into logical steps that the computer can follow.

An Example Of Algorithmic Thinking

How many of you prepare coffee/tea in the morning? Break down your process into steps. Now break it down into more steps. When we communicate to humans, we can skip some information because it is implied. We can’t do this in programming.

CPU instructions: Assembly

Assembly

Example of Assembly code, which is a low-level language (very close to the hardware). Even this has to be compiled into machine code…

Introduction to Python

  • Python is a high level programming language, it is very far from assembly indeed. Created in 1990, it is a modern (but mature) language.
  • It’s meant to be human readable as much as possible.
  • It is interpreted, not compiled. Each instruction is translated to action immediately, rather than being converted as a file.
  • It has a large standard library: basically it can do a lot of things, but in many cases is not the ideal tool for the job. Python is considered to be a very good general-use programming language.

Installing Python and an IDE

  • Create a Virtual Machine (Fedora Workstation is recommended)
  • Python is already installed by default on Fedora.
  • You will also need to install VS Code

Introduction To Python

There are two ways to use Python:

  1. The Interpreter (IDLE). You enter lines of code, a result appears instantly
  2. Scripts. You enter many lines of code into a .py file, and run the file as an executable

Generally, the interpreter is good for trying things out, but we will be focusing on creating scripts.

Printing

print("Hello World!")

This code will print the string to your terminal.

Strings

x = "this is a string"

Notice the quotes around the words, this is important! You can also use single quotes (’’).

Inputting

x = input("Please enter your name: ")

input is a way for us to prompt the user for information. This is a function.

Variables

x = 3  # integer: whole number
word = "Hello"  # string: a series of characters
square_root_of_2 = 1.4142  # float: a number with a fractional component

We often need to store pieces of data. We use variables to do this. On the left, the variable name. On the right, the value assigned to it. = separates the two.

Here we have an integer, a string, and a float.

Functions

Functions are named blocks of code. Python has many, many builtin functions. Functions often have input (arguments) and output (return values). Let’s look at an example:

x = input("Enter your name: ")

Functions

x = input("Enter your name: ")

In this example, the function named input takes a string as an argument (inside the parentheses) and is returning another string, which we can store in a variable called x.

Your First Script

#!/usr/bin/env python3

'''
Your first program.
Author: Joe Seneca
'''

name = input("Please enter your name: ") # prompts the user for a name
print("Hello " + name + "!") # + used to concatenate strings

Scripting Guidelines

  • All Python scripts should end in ‘.py’ as their suffix (or file extension).
  • This identifies the file as a Python script, and not just a text file.

Example: lab1a.py

  • All Python scripts should have a shebang: #!/usr/bin/env python3
  • This distinguishes Python 3 code from Python 2, which is quite different and won’t run correctly.

Commenting Code

Comments are very important, but often overlooked by students!

Explains the code to other programmers, and to yourself (after time has passed).

#
starts an in-line comment (one sentence long).
'''
will create a docstring (a paragraph or longer).

Commenting Code II

Writing comments is an art. But generally, don’t explain what your code does (it’s usually obvious). Explain why your code is doing what it does.

Wrong

Built-In Functions In The Interpreter

The Python Interpreter also has some built-in functions that you may find useful.

help()
will print the documentation for a function or object
type()
will tell you what datatype an object is considered to be

The good (and bad!) thing about Python is that datatypes are implicit.

That means they aren’t obvious (but will still cause you issues!)

End