TUTORIAL 1
234128 – Introduction to
Computing with Python
Written and edited by Bronislav Demin
Today
• Paths and Directories
• Anaconda Command Prompt
• Python 3
• Input\Output
• Spyder IDE
• Problem Solving
2
Before you continue:
It is recommended to install Anaconda through the guide on
Moodle
3
PATHS AND DIRECTORIES
4
Operating System (OS)
• Every computer must have an operating system, from super
computers to personal computers and cellular phones.
• An Operating System is a Computer Program like any other.
• It offers various services and “Quality of life” improvements for the
human user:
• Handling files, input/output devices.
• Multitasking – Running multiple programs and applications at once.
• Having various built-in utilities: Browser, calculator, sketch board, etc.
5
Data Organization
• Data is stored inside a computer as a sequence of bits.
• Every file is a sequence of bits.
• A computer holds large number of different files (hundreds of
thousands and more)
How to organize all this data? Memory
Files
Data
0101100
6
Files
• A file is addressed by a filename.
• Filenames are composed of name dot extension: Name
. Extension
• With what file types are you familiar with?
• For example: my_file.doc
• The file extension defines the type of content inside the file:
- Text and Word files (.txt .doc)
- Images (.gif .jpg .bmp .png)
- Media (.avi .mpg .mp3 .mp4 .wav)
- Programs (.exe) A collection of commands
stored as Machine Code
7
Directory Structure
Main partition, or the “root” of the
C: disk. Marked with letter and colon
home my pictures song.mp3
ex2.py ex1.py
progs
my_dog.jpg my_cat.gif It’s common to display files and
partitions as a tree diagram
ex1.txt ex1.py
8
Path
C:
home my pictures song.mp3
ex2.py ex1.py my_dog.jpg my_cat.gif
progs
The path to this ex1.py file is:
ex1.ipynb ex1.py
C:\home\progs\ex1.py
We use the “\” separator between different
subfolders
9
ANACONDA PROMPT
10
Anaconda Prompt
• Press Start
• Find Anaconda3
• Find and launch Anaconda Prompt
11
Anaconda Prompt
• The command Prompt is another way to interact with the operating
system.
• To change color, font size:
• Alt+Enter for full screen.
Right Click on the top
bar → Properties
12
Current Directory
• When opening the command prompt, it is waiting for instructions.
• The current working directory is always visible:
• After typing a command, we press Enter for execution.
13
Change Directory
• To change the current working directory, use the cd name command:
• “cd” – short for change directory
• name – the name of the folder
• To go back one level, use cd.. command:
• You can go to the root folder by using cd..
command multiple times:
14
Errors
• What if we try to change to a directory which doesn’t exist?
• We got an Error
• That’s fine and expected. We can continue typing commands as
usual.
15
Directory Contents
• To view the contents of a directory simply type the command dir:
16
Lower/Upper Case?
• You can type cd, CD, cD, or Cd. Any combination will work.
• Windows OS doesn’t distinguish between upper-case and lower-case
letters.
• Later in the course we will see that in some cases, it DOES make a
difference.
• Particularly, in Python:
17
PYTHON 3
What is Python?
• Python is a high-level general purpose programming language.
• Known for its vast selection of standard and 3rd-party libraries
• Commonly used for:
• Scientific uses (Free Matlab\Excel alternative)
• Data Analysis and Visualization
• Web Development
• Computer Vision
• Machine Learning
• Scripting and Automation
19
Python REPL
• REPL (Read Evaluate Print Loop) – Python command entry and output
display.
• Type python in the Anaconda Prompt to launch the Python REPL:
• You can experiment freely with Python code inside the REPL.
• Press CTRL+Z and Enter to return to the Anaconda Prompt.
20
My First Program
• The print function tells the computer to print what is inside the
quotes.
• By default, the function adds a new line after the printed text.
21
Running a Program
• Instead of running single commands, we can run a whole program
through Python.
• A program is simply a collection of instructions, stored as text.
• To run a Python program inside the Anaconda Prompt use:
python my_program.py
• python – run the following program through the Python Interpreter
• my_program.py – the name of the program you want to run
22
INPUT/OUTPUT
Input/Output
Output
device program
Input device
Displaying output Feeding input
• A program can receive information from the user as input.
• A program can display information for the user as output.
• The keyboard and the display are the default means for input and output.
24
Input/Output
• Sometimes we want to get
input from the user.
• We can use the input()
function in Python.
• This function can include
helpful text to be displayed for
the user.
• As seen before, for output
we can use the print function.
The computer is
Our typed waiting for keyboard
input input
25
Input/Output Redirection
• It is possible to send/receive input/output to/from a Python
program via text files. This is called Input/Output Redirection.
• The main use (in this course) of this will be in the homework
assignments.
• You will receive text files which include input to be used in your
programs with expected output.
• After creating the program, you will check if your resulting output
matches the expected output.
• This will help you avoid typing the input every time you want you
check your program (especially if the input is long) and save time.
26
Input From File
• Consider our simple program:
• Let’s assume this program is stored inside a file called tut1.py
• By using input redirection, we can supply input to this program from
an external text file. Here is the input file in.txt:
• You can create this file by using Notepad or any other way.
27
Input From File
• To redirect the input into our program, we will run our program via
the Anaconda Prompt in a specific way:
• After the name of the program, we will add: < in.txt
Take input from
in.txt
The program displayed
output without requiring
typed input
28
Input From File
• Similarly, we can redirect output from a program to an external file.
• After the name of the program, we will add: > out.txt
Send output to
out.txt
Typed input is redirected
into out.txt
• out.txt will be created automatically:
29
Why Not Both?
• We can combine both methods to redirect input/output to/from text
files:
Send output to Get input from
out.txt in.txt
• Notice that the program ran without typing input or displaying any
output. This is due to redirection!
30
Input/Output Redirection Summary
• Reading input from input.txt instead of the keyboard:
python example.py < input.txt
• Writing output to output.txt text file:
python example.py > output.txt
• Combining both (order doesn’t matter):
python example.py < in.txt > out.txt
python example.py > out.txt < in.txt
SPYDER
Spyder
• Anaconda – A simple installation package for Python.
• IDE (Integrated Development Environment) – is special software
made of tools for building and developing software. Basically, a fancy
text-editor.
• In this course we will use Spyder IDE through the Anaconda
platform.
33
Spyder: Editor
34
Spyder: Editor
• The Spyder Editor is where we
type code or create programs
• It is similar in nature to Notepad or
Word. Code is nothing more but text!
• You can work on multiple files at
once
The Spyder Editor includes useful
functionality:
• Syntax highlighting
• Linting – automatic check for errors
• Debugging – will be discussed later in
the course.
• And more…
35
Spyder: Editor
• Change font size quickly (works
also in Anaconda Prompt):
First make sure the editor window is
selected. Hold CTRL on the
keyboard and rotate the mouse
wheel (or swipe up/down on the
trackpad with two fingers)
• Change themes, colors and design:
36
Spyder: Console
37
Spyder: Console
• The Spyder Console is similar in
nature to what we did in the
Command Prompt.
• It acts as an IPython Console
and a Command Prompt.
• You can type various commands,
run programs and execute Python
code.
• It is encouraged to experiment
as much as you can!
38
Spyder: Help
39
Spyder: Help
• You can search various keywords (functions, commands, modules, etc)
and receive detailed information.
• Useful for exploring additional functionality or refresh your memory if
you forgot something.
40
Spyder: Variable Explorer
41
Spyder: Variable Explorer
• The Variable Explorer keeps track of the current variables that are
stored in memory.
• Will help us tremendously with debugging later in the course.
42
STEPS IN PROBLEM SOLVING
234128 – Introduction to Computing with Python 43
Problem Solving: The Steps
• Understand the problem.
Identify the Input → Output
• Design an algorithm (a recipe) to
solve it
• Start coding
234128 – Introduction to Computing with Python 44
Problem Solving: Cashier
Example
Write a program which acts as a Cashier:
• Receives a series of purchases
• Prints the current total
• Tells the cashier to go home when the total reaches over $1000
234128 – Introduction to Computing with Python 45
Problem Solving: Algorithm
1. Define a counter which keeps track of the current sum.
sum=0
2. Get the cost as input
Add the cost to the sum Get x
Display the sum sum = sum+x
Print sum
3. If the sum is smaller than $1000, repeat
yes
sum<1000?
4. If not, display “Go Home!” no
Print ‘Go Home!’
234128 – Introduction to Computing with Python 46
Problem Solving: Code
Example:
Write a program which
receives a series of purchases,
prints the current total and
tells the cashier to go home
when the total reaches over
$1000.
234128 – Introduction to Computing with Python 47
Actual Coding and Debugging?
Next
Week….
234128 – Introduction to Computing with Python 48
49