Learning to Program in Python

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

Tutorial 8: Developing an Excel Application
Introduction to Python
Chapter 1 Program Design
MBAC 611.  We have been using MS Access to query and modify our databases.  MS Access provides a GUI (Graphical User Interface) that hides much of the.
Introducing Java.
Noadswood Science,  To understand what strings are and how decisions are made Thursday, September 17, 2015.
Section 2 Variables National 4/5 Scratch Course. What you should know after this lesson What a variable is Where variables are stored How to get data.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Describe the Program Development Cycle. Program Development Cycle The program development cycle is a series of steps programmers use to build computer.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Introduction to Strings CSIS 1595: Fundamentals of Programming and Problem Solving 1.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
Getting Started With Python Brendan Routledge
PROBLEM SOLVING WARM-UP Fill in the spaces using any operation to solve the following (!, (), -/+,÷,×): = 6.
IS 350 Course Introduction. Slide 2 Objectives Identify the steps performed in the software development life cycle Describe selected tools used to design.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Development Environment
Topic: Python Lists – Part 1
Formatting Output.
Tuples and Lists.
Introduction to Programming
CMPT 120 Topic: Python strings.
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Intro to PHP & Variables
Engineering Innovation Center
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
MODULE 7 Microsoft Access 2010
Learning to Program in Python
Learning to Program in Python
Creativity in Algorithms
Introduction to Programming
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Post 16 Return SEPTEMBER 2012.
Learning to Program in Python
Teaching London Computing
Learning to Program in Python
Learning to Program in Python
4. sequence data type Rocky K. C. Chang 16 September 2018
Introduction to TouchDevelop
Module 4 Loops.
A look at Small basic The Text Window 2017.
Introduction to Programming
Learning to Program in Python
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Introduction to Computer Science
Introduction to Programming
Post 16 Return SEPTEMBER 2011.
Unit 3: Variables in Java
Introduction to Python
Lesson 02: Introduction to Python
Introduction to Computer Science
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Learning to Program in Python Strings are a form of data type. The main Learning Outcomes (LO) achieved are highlighted throughout the lesson and tasks. The list is not exhaustive, and consultation with the LCCS spec is encouraged at all times throughout the implementation of the course. Concept 3 STRINGS

Learning Intentions From this lesson the students will be able to: Understand the concept of a string Retrieve and Replace characters in strings Slice a string into sub-strings Write strings to a file The skills learned to analyse and create strings will serve the students well for conditional statements and also for learning lists. LO 2.16 students should be able to use data types that are common to procedural high-level languages

STRINGS Our very first line of code we wrote in the IDE was: “hello world” is an example of a string. You can enclose it in single quotes also. (‘hello world’) Link to very first use of python in class. Encourage use of double quotes.

(These characters are in fact strings of length 1) A string is a data type. Each individual member of a string is a character such as “A” or “1” or “!” or “ “. (These characters are in fact strings of length 1) Emphasise the first index is ZERO and strings are a form of data type ….. Very popular type in Python

STRINGS The characters can be accessed by their index or place in the string. The first index is ZERO Let’s try some examples in your IDE Emphasise the first index is ZERO and strings are a form of data type ….. All basic input and output in Python is in the form of strings

Go to your shell (IDE) and figure out some of these String Methods Highlight slicing [5:9] or [:4] etc….. Addition of strings …….. And strings are fixed or immutable LO 2.20 students should be able to identify and fix/debug warnings and errors in computer code and modify as required

Notice that the last command below reports an error. The string cannot be assigned a value in this way. Strings are said to be IMMUTABLE. (See the Python Docs in your Python Shell. Go to the Tutorial) Allow students time to preview the Python Docs and especially The Python Tutorial. So to change a string you must make a copy, and change it as you construct the new string.

Escape Characters( \ ) Remember : If we want to print a string and then on the next line print another string we use the backslash followed by the letter n. print(“First Line.\nSecond Line.”) (backslash (\) is known as an escape character in Python) Encourage the students to experiment and continue to research the Python Docs -> Tutorial -> Strings

Escape Characters( \ ) “\t” Inserts a tab for example “\”” The backslash here escapes the quotes and so this would print out a set of quotes. (See the Strings section in Python Docs Tutorials.) Encourage the students to experiment and continue to research the Python Docs -> Tutorial -> Strings

Length of a String (Len) A very useful function in programming is finding the Length of a string. Try the code below. Encourage the students to type in this code, save in a file in their storage space, and edit and experiment LO 1.22 students should be able to read, write, test, and modify computer programs

a = ‘Happy’ b = ‘Birthday’ + Concatenation a + b gives ‘HappyBirthday’ * Repetition a * 3 gives ‘HappyHappyHappy’ [] Slice b[1] gives ‘i’ [:] Range Slice a[:2] + b[5:] gives ‘Haday’ = Assignment c = b gives c =‘Birthday’ and c is separate from b. += Assignment & concatenation c += ‘ Boy’ gives c as ‘Birthday Boy’ in Membership ‘p’ in a gives True (or 1) ‘q’ in a gives False (or 0) The word descriptions of the operations are important. (Slice, Concatenation, Assignment, etc….) Encourage students to predict the outcomes. The PRIMM method will encourage engagement. (Predict Run Investigate Modify Make) The assignment and concat are useful for the solution to the next challenge. False ‘t’ in a gives ?

What line of code could be behind me? What line of code is behind me? Encourage the students to type in this code, save in a file in their storage space, and edit and experiment. They need to read all the lines of code first, especially the last 3 in order to figure out the lines that are hidden. What line of code could be behind me?

CHALLENGE 1. Ask the user to type in a 4 letter word For example “mart” 2. If the length of the word is not 4, ask user again. Reverse the string and output as a new string From the example above : “tram” Ask the students to perform this task …. The assumption at this point is they have not studied loops; when loops are studied, this can be re-visited. The Scratch symbol indicates that this task should also be done in Scratch (or similar block-based language). The html resource, containing the Scratch program and guiding the students, is part of the same folder as this Python resource. Encourage the students to edit the html file to serve as part of an ePortfolio. See the file for instructions. NOTE The computational thinking challenge in this section asks the students to combine this code with code already written to display words using a Graphical User Interface (GUI) using Tkinter modules. LO 1.7 students should be able to develop algorithms to implement chosen solutions

CHALLENGE How would you output result to a txt file? 1. Ask the user to type in a 4 letter word For example “mart” 2. If the length of the word is not 4, ask the user again. Reverse the string and output as a new string From the example above : “tram” How would you output result to a txt file? Ask the students to perform this task …. The assumption at this point is they have not studied loops; when loops are studied, this can be re-visited; NOTE The computational thinking challenge in this section asks the students to combine this code with code already written to display words using a Graphical User Interface (GUI) using Tkinter modules. LO 1.7 students should be able to develop algorithms to implement chosen solutions

Hint for the Challenge The Brain Teaser on the next slide addresses 2 issues : 1. Interactive code must anticipate errors by the user. So what does your code do when more than 4 letters are entered? 2. Since the length of the string is an unknown, the problem cannot be solved without a loop of some description. There is value in challenging the students to find a solution without loops, so creating a necessity for loops to solve more interesting problems and create better programs.

Brain Teasers 1. If the user typed in a string of length 5, what would your program do? 2. How would you change your program to handle a string of any length? ( Remember the len()method. ) The Brain Teaser addresses 2 issues : 1. Interactive code must anticipate errors by the user. So what does your code do when more than 4 letters are entered? 2. Since the length of the string is an unknown, the problem cannot be solved without a loop of some description. There is value in challenging the students to find a solution without loops, thus creating a necessity for loops to solve more interesting problems and create better programs. LO 2.21 students should be able to critically reflect on and identify limitations in completed code and suggest possible improvements

Lesson Review As a result of this lesson I am able to: Understand the concept of a string Retrieve and Replace characters in strings Slice a string into sub-strings Write strings out to a file The computation challenge for this section asks the students to : Consider their code to reverse a 4 letter string Combine their code with code already written for a User Interface (UI) Modify the new program to ensure correct functionality The last learning intention achieved will be built upon as lessons develop. Basic database building will be developed in lessons on functions and files. The CT challenge is on a html platform. It gives an existing algorithm for a Graphical UI. Students can edit the html code, including reflections on their design, thus serving as a record of the progress and development. LO 2.9 students should be able to assemble existing algorithms or create new ones that use functions (including recursive), procedures, and modules