0% found this document useful (0 votes)
3 views17 pages

COMP1021 - Note 2 (Beginning To Program Python)

This document provides an introduction to programming in Python, covering basic concepts such as running code in a shell, handling text input and output, and using variables. It explains how to store user input in variables, convert text to numbers, and generate random numbers. Additionally, it discusses the importance of organizing code into programs for efficient execution.

Uploaded by

yue min
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

COMP1021 - Note 2 (Beginning To Program Python)

This document provides an introduction to programming in Python, covering basic concepts such as running code in a shell, handling text input and output, and using variables. It explains how to store user input in variables, convert text to numbers, and generate random numbers. Additionally, it discusses the importance of organizing code into programs for efficient execution.

Uploaded by

yue min
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COMP1021

Introduction to Computer Science

Beginning to
Program Python

David Rossiter
Outcomes
• After completing this presentation, you are
expected to be able to:
1. Run one line of code at a time in the shell
2. Run several lines of code as a program
3. Use code to do simple text input and output
4. Use variables to store things, such as text and
numbers

COMP1021 Beginning to Program Python Page 2


How to Run Python?
• We will talk about two approaches:
1. You run Python 2. You run Python
code in the shell code in a program

See the last slide


In the world of computers, a shell means
a place where you run one line of code, then
you see the result of running that line of code
Input and Output

Input Python Code Output

• In this presentation • In this presentation


we’ll look at text input we’ll look at text output
• Later we will look at • Later we’ll look at some
handling some other types other types of output
of input such as mouse input such as graphics output
and music output
COMP1021 Beginning to Program Python Page 4
Text Output
• Let’s do some simple text output
• Here is a line of Python code which shows
a message on the screen:
1.
print("Today is a hot day!")

• This is the print command • This is the message that we want


that asks Python to show to show on the screen
something on the screen • When you use text in code, you
• You put the message you need to enclose the text using a
want to show inside a pair pair of quotes, "" or ''
without quotation marks and parenthses > syntax error
of parentheses, i.e. () without quotation marks > syntax error
one double & one single quotation marks > syntax error
COMP1021 Beginning to Program Python Page 5
Using the Shell
• When you start IDLE you see the shell
• If you type the code into the shell then press Enter, the
code is given to the interpreter and the result is shown:

The result of your code is


shown here, under the code
>>> means ‘next to this is your code’
Text Input
• Let’s do some text input
• Here is a line of Python code which shows a
message and lets the user enter something:
2. input("What is your name?")

• This is the input command • This is the message that we


which: want to show on the screen
• asks Python to show
something on the screen, and
• returns whatever the user types

COMP1021 Beginning to Program Python Page 7


Remembering Things
• We need a way to remember what the user enters
• To do that we use a variable
• You can think of a variable as a box
• When you do any variable
3. variable_name = input( . . . )
then whatever the user types is stored
in the box

variable_name

COMP1021 Beginning to Program Python Page 8


The >>> means
we are using Using a Variable “Dave”
the shell; you name
can ignore • Here is some code which stores any
that part text the user enters in a variable:
name = input("What is your name?")
variable name 1. We give the
Python
interpreter
this code
2. The Python
interpreter 3. The user
executes the 4. The Python interpreter stores the types in
code, we see input in the variable called ‘name’ some input
the message

COMP1021 Beginning to Program Python Page 9


Accessing the Variable
variable : no quotation marks

• If we want to use whatever is in the variable, we


simply use the name of the variable
• For example, let’s use print() to show what’s
in the variable:
name is being substiuted by Dave

• We could mix it with some text, like this:

output
or this:
What About Entering Numbers?
• If we want to get a number from the user,
we can use the same code input()
• However, input() always produces text
•variable=money
The code will crash if you try to treat a variable
which has text as if it has a number e.g.:

input

a text a no.
Converting Text into a Number
100
• What we can do is to take the input from the user,
and then convert it to a number using int()
• int() means ‘convert this into an integer’
• After it has been converted, you can add, subtract,
multiply, etc, the number stored in the variable

can combine: integeralized and addition


int(money)+5
Convert the text “100”
4.
into an integer 100

addition
Generating a Random Number
• Sometimes it is useful to ask Python to give you
some random numbers
• There are several ways to do that in Python
• One of them is to use the [Link]()
command
• First, we need to use this code:
5. import random
• After this, your code can use lots of commands
related to random numbers
COMP1021 Beginning to Program Python Page 13
Generating a Random Number
• Then we can use [Link]() to generate
a random number within a particular range, like this:
6.
• This says ‘generate
a random number
which is 1 or 2 or
3 or 4 or 5 or 6 or
7 or 8 or 9 or 10’
1. r1 = [Link](1,10)
2. print(r1)
5

• We will use this to generate random numbers later


Putting Lines of Code Together
• Typing lines of code in the shell is OK but you
may want to run the same lines of code many times
• You will go crazy if you have to keep typing them!
• It makes sense to put all the lines of code together
into a single file of Python code
• That file, often containing many lines of code,
is called a program

COMP1021 Beginning to Program Python Page 15


Making and Running a Program
(Windows)
Create the program

Save the program Run the program

• This slide shows


IDLE being used The result is shown
on Microsoft
Windows
Making and Running a Program
(Mac)
Create the program

Save the program Run the program

• This slide shows


IDLE being used The result is shown
on a Mac

You might also like