Beginning to Program Python



Similar documents
University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

CS 1133, LAB 2: FUNCTIONS AND TESTING

Introduction to Python

Supplemental Worksheet Problems To Accompany: The Pre-Algebra Tutor: Volume 1 Section 9 Order of Operations

Welcome to Introduction to programming in Python

Programming in Access VBA

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Computer Science for San Francisco Youth

Practical Programming, 2nd Edition

Computer Programming In QBasic

Fractions. If the top and bottom numbers of a fraction are the same then you have a whole one.

Year 9 set 1 Mathematics notes, to accompany the 9H book.

Graphic Objects and Loading Them into TGF2/MMF2

Lab 1 Beginning C Program

Microsoft Access XP Session 1 Week 8

Factoring Quadratic Expressions

Chapter 1: Order of Operations, Fractions & Percents

CS 145: NoSQL Activity Stanford University, Fall 2015 A Quick Introdution to Redis

Level 15: Creating a Puzzle

Safeguard Ecommerce Integration / API

CSCE 110 Programming I Basics of Python: Variables, Expressions, and Input/Output

Python Lists and Loops

MICROSOFT EXCEL FORECASTING AND DATA ANALYSIS

Simplification Problems to Prepare for Calculus

MA 1125 Lecture 14 - Expected Values. Friday, February 28, Objectives: Introduce expected values.

Teaching Pre-Algebra in PowerPoint

EVALUATING ACADEMIC READINESS FOR APPRENTICESHIP TRAINING Revised For ACCESS TO APPRENTICESHIP

Appendix: Tutorial Introduction to MATLAB

Using an Android Phone or Tablet For Your Speech / Video Submission Assignment

HowTo. Planning table online

13 Managing Devices. Your computer is an assembly of many components from different manufacturers. LESSON OBJECTIVES

Simple Examples. This is the information that we are given. To find the answer we are to solve an equation in one variable, x.

WAYNESBORO AREA SCHOOL DISTRICT CURRICULUM INTRODUCTION TO COMPUTER SCIENCE (June 2014)

Using Microsoft SyncToy to Automate Backups

mouse (or the option key on Macintosh) and move the mouse. You should see that you are able to zoom into and out of the scene.

Pigeonhole Principle Solutions

Collaborating Across Disciplines with Revit Architecture, MEP, and Structure

Square Roots and Other Radicals

Week 2 Practical Objects and Turtles

Solution Guide Chapter 14 Mixing Fractions, Decimals, and Percents Together

Prescribed Specialised Services 2015/16 Shadow Monitoring Tool

Managing your Inventory

Activity 1: Using base ten blocks to model operations on decimals

Creating Your Own TinyWeb Database. Ball State University - CS116 - Ashley Swartz

Task Scheduler. Morgan N. Sandquist Developer: Gary Meyer Reviewer: Lauri Watts

Understanding class definitions

3. Solve the equation containing only one variable for that variable.

Algebra 2 Notes AII.7 Functions: Review, Domain/Range. Function: Domain: Range:

Introduction to Python

Notepad++ The COMPSCI 101 Text Editor for Windows. What is a text editor? Install Python 3

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

CS170 Lab 11 Abstract Data Types & Objects

LESSON 7: IMPORTING AND VECTORIZING A BITMAP IMAGE

Python Loops and String Manipulation

Title: SharePoint Advanced Training

MATLAB Functions. function [Out_1,Out_2,,Out_N] = function_name(in_1,in_2,,in_m)

Introduction to Python

CS 100 Introduction to Computer Science Lab 1 - Playing with Python Due September 21/23, 11:55 PM

Using Windows Task Scheduler instead of the Backup Express Scheduler

Creating QBE Queries in Microsoft SQL Server

Extreme computing lab exercises Session one

AUSTRALIAN CUSTOMS AND BORDER PROTECTION SERVICE TYPE 3 CERTIFICATE 2014 INSTALLATION GUIDE

Android App Development Lloyd Hasson 2015 CONTENTS. Web-Based Method: Codenvy. Sponsored by. Android App. Development

RIT Installation Instructions

Base Conversion written by Cathy Saxton

Programming LEGO NXT Robots using NXC

Databases in Microsoft Access David M. Marcovitz, Ph.D.

Understanding DPI and Pixel Dimensions

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Section 4.1 Rules of Exponents

The first program: Little Crab

MARS STUDENT IMAGING PROJECT

Factoring Trinomials of the Form x 2 bx c

Accentuate the Negative: Homework Examples from ACE

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Introduction to SQL for Data Scientists

NF5-12 Flexibility with Equivalent Fractions and Pages

Simulating Investment Portfolios

How to Setup Privacy Guard Encryption.

The theory of the six stages of learning with integers (Published in Mathematics in Schools, Volume 29, Number 2, March 2000) Stage 1

Advanced Programming with LEGO NXT MindStorms

2 The first program: Little Crab

Programming Languages CIS 443

Mapping the ITS File Server Folders to Mosaic Windows

The 2010 British Informatics Olympiad

Using Subversion in Computer Science

FileBench's Multi-Client feature

Training Modules: Entering a New Transcript Part 2

CS 241 Data Organization Coding Standards

Opposites are all around us. If you move forward two spaces in a board game

Microsoft Access 3: Understanding and Creating Queries

Factoring. Factoring Monomials Monomials can often be factored in more than one way.

Comp 255Q - 1M: Computer Organization Lab #3 - Machine Language Programs for the PDP-8

Introduction to Simulink

Transcription:

COMP1021 Introduction to Computer Science Beginning to Program Python David Rossiter

Outcomes After completing this presentation, you are expected to be able to: 1. Use Python code to do simple text input and output 2. Use variables to store things, such as text and numbers 3. Demonstrate running Python code as a program COMP1021 Beginning to Program Python Page 2

Input and Output Input Python Code Output In this presentation we ll look at text input Later we will look at handling various other types of input such as mouse input In this presentation we ll look at text output Later we ll look at some graphics and music output COMP1021 Beginning to Program Python Page 3

Text Output Let s do some simple text output Here is a line of Python code which prints (i.e. output) a message: print("it's a typhoon!") This is the print command that asks Python to show something on the screen You put the message you want to show inside a pair of brackets, i.e. () This is the message that we want to show on the screen When you use text in Python code, you need to enclose the text using a pair of double-quotes, i.e. "" COMP1021 Beginning to Program Python Page 4

Text Output print("it's a typhoon!") If we type the code directly into the shell, it immediately gets executed and the result is shown: You can tell Python to print anything you like COMP1021 Beginning to Program Python Page 5

Text Input Let s do some simple text input Here is a line of Python code which shows a message and lets the user enter something: input("what is your name?") This is the input command which: asks Python to show something on the screen, and returns the thing that the user types This is the message that we want to show on the screen COMP1021 Beginning to Program Python Page 6

Text Input input("what is your name?") If we type this code directly into the shell, it is immediately executed, the message is shown and the result the user types is returned: 1. We give the Python shell this code 2. The Python shell shows the text 4. The Python interpreter returns the result of the input, and shows it 3. The user types in some input COMP1021 Beginning to Program Python Page 7

Remembering Things In the example we just looked at, whatever the user typed is completely forgotten after Python has finished processing the input command Although you can still see things on the screen, Python doesn t remember that It will be very useful if we have some way to remember what the user types To do that we need to use a variable to store it COMP1021 Beginning to Program Python Page 8

Using a Variable You can think of a variable as a box Here is some code which stores whatever text the user enters in a variable: name=input("what is your name?") Dave Name 2. The Python interpreter shows the text 4. The Python interpreter stores the input in the variable called name 1. We give the Python interpreter this code 3. The user types in some input COMP1021 Beginning to Program Python Page 9

Accessing the Variable 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: We could mix it with some text, like this: or this:

What About Entering Numbers? If we want to get a number from the user, we can use the same input function input() However, input() always produces text You will encounter a problem if you try to treat the variable as if it has a number, like this: COMP1021 Beginning to Program Python Page 11

Converting Text into a Number 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 Convert the text 100 into an integer 100 COMP1021 Beginning to Program Python Page 12

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 random.randint() command First, we need to use this code: import random This code tells Python that we want to use a command related to random numbers COMP1021 Beginning to Program Python Page 13

Generating a (Random) Number Then we can use random.randint() to generate a random number within a particular range, like this: We will use this technique to generate random numbers for games later COMP1021 Beginning to Program Python Page 14

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, usually containing many lines of code, is called a program COMP1021 Beginning to Program Python Page 15

Making and Running a Program Create the program Save the program Run the program When you write the filename, remember to add the.py extension The result is shown