Noadswood Science, 2014.  To understand what strings are and how decisions are made Thursday, September 17, 2015.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

P1PMF Split1 QBASIC. P1PMF Split2QBasic Command Prompt Will launch the emulator DOS operating system? Press Alt + Enter to display the widescreen.
Introduction to Computing Science and Programming I
Introduction to C Programming
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to Python
JavaScript, Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Fundamentals of Python: From First Programs Through Data Structures
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Fundamentals of Python: First Programs
Noadswood Science,  To know the basics of Python coding and decoding Monday, September 07, 2015.
An Introduction to Textual Programming
Introduction to Python
CATHERINE AND ANNIE Python: Part 4. Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Introduction to Programming with RAPTOR
Lesson 6. Python 3.3 Objectives. In this lesson students will learn how to output data to the screen and request input from the user. Students will also.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Decision Structures, String Comparison, Nested Structures
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
Strings The Basics. Strings a collection data type can refer to a string variable as one variable or as many different components (characters) string.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Formatting Output. Line Endings By now, you’ve noticed that the print( ) function will automatically print out a new line after passing all of the arguments.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Input, Output and Variables GCSE Computer Science – Python.
ENGINEERING 1D04 Tutorial 1. WELCOME! What we’re doing today Who am I How to do well What are computer programs Software Development Cycle Pseudo Code.
GCSE COMPUTER SCIENCE Practical Programming using Python
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Formatting Output.
IF statements.
Introduction to Programming
Variables, Expressions, and IO
Computation with strings 3 Day 4 - 9/07/16
Engineering Innovation Center
Decision Structures, String Comparison, Nested Structures
Learning to Program in Python
Introduction to C++ Programming
Decision Structures, String Comparison, Nested Structures
An Introduction to Python
CS 100: Roadmap to Computing
Variables & getting info from the user
Introduction to Computer Science
Primitive Types and Expressions
Python Strings.
Data Types and Maths Programming Guides.
While Loops in Python.
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Noadswood Science, 2014

 To understand what strings are and how decisions are made Thursday, September 17, 2015

Strings  Python is excellent for using words and sentences within programs: different strings can be joined together, or individual parts of them can be selected and pulled out…  Remember, a string can include letters, numbers, symbols or spaces a = ‘Run! ’ b = ‘Aliens are coming.’

Adding Strings  Adding two numbers together creates a new number, and in the same way two strings can be added together (with one simply joining the other)  The “+” symbol joins one string to the other a = ‘Run! ’< notice the space here after ! b = ‘Aliens are coming.’ c = a + b print(c)

In Between String  New strings can also be added between two strings a = ‘Run! ’ b = ‘Aliens are coming.’ c = b + ‘ Watch out! ‘ + a print(c)  Consider where your spaces go!

String Length  Python allows you to identify the length of a string using the “len()” function  It will count all the characters (including spaces) to give the total number of characters in a string a = ‘Run! ’ b = ‘Aliens are coming.’ print(len(b))  The answer should be 18 (there are 18 characters in string “b”)

String Escape  Strings can go in single or double quotes – it is important that strings start and end with the same type of quote  Write the following string print(‘It’s a lovely day today.’)  This will cause an error – in order to avoid this Python has a function known as escaping – within the string if an apostrophe is needed type “\” before it such as the code below print(‘It\’s a lovely day today.’)

Numbering Characters  Each character in a string is allocated a number according to its position – this position number can be used to look at individual letters or symbols, or to pull them out of a string…  When counting the positions, Python starts at 0 (the second character is 1, the third is 2 etc…) FLAMINGO

Numbering Characters  The position number is known as the “index” and can be used to pull out a particular letter from a string FLAMINGO a = ‘FLAMINGO’ print(a[3])< the character is position 3 from variable “a”

Slicing  Two indexes can be used to pull out a part of the string (slice it) – the letter in the last position isn’t included FLAMINGO a = ‘FLAMINGO’ print(a[3:7])< a slice from index 3 to 6 in variable “a”

Start to End  If you leave off the start or end index, Python will automatically use the first or last character of the string FLAMINGO a = ‘FLAMINGO’ print(a[:3]) print(a[3:])

Input  The “input()” function is used to accept input from the keyboard into a program – it waits until a user finishes typing and presses the return or Enter key name = input (‘Enter your name: ‘) print(‘Hello’, name)

Output  The “print()” function is used to display characters in the shell window – it can be used to show a combination of text and variables a = ‘David’ b = ‘is’ c = 31< no quote marks as this is an integer print(a, b, c) print(‘Goodbye’, a)

Separating Strings  Strings can be separated in a variety of ways, including using the separator command (“sep”)  Strings can be separated using spaces  Strings can be separated using hyphens “-” (or other characters such as “+” or “*”)  Strings can also be separated by printing on a new line

Separating Strings a = ‘David’ b = ‘is’ c = 31 print(a, b, c)< separates using a space print(a, b, c, sep=‘-’)< separates using a hyphen print(a, b, c, sep=‘\n’)< separates onto a new line

Ending Output  There are several ways to signal the end of an output of a “print” function a = ‘Hurray!’ print(a, ‘.’)" print(a, end=‘.’)< “end=‘.’” ensures no space is printed print(end='\n')< new line for n in range(3):< repeat x3 print(a, end=‘ ‘)< output all on one line using a space print(a, end=‘\n\n\n\n’)< “\n” starts a new line print('4 new lines')

Decisions  Programs make decisions about what to do by comparing variables, numbers and strings using Boolean expressions (True or False)  Logical operators are used to compare variables against numbers, strings or other variables – the result is either True or False ==(equals operator) !=(not equal operator) <(less than operator) >(more than operator) <=(less than or equal to operator) >=(more than or equal to operator) *Remember, Python uses the equals sign (=) to assign a value to a variable

Decisions  Write a Python code for a variable (toys) which equals the integer of 10  Print off the results for equal, not equal, less than, more than, less than or equal to and more than or equal to operators toys = 10 print(toys == 1) print(toys != 1) print(toys < 1) print(toys > 1) print(toys <= 1) print(toys >= 1)

Not, Or, And  The not logical operator (not) reverses the answer (from True to False and vice versa)  The or logical operator (or) checks if the outcome is one or another  The and logical operator (and) checks if the outcome is one thing and another toys = 10 print(not toys == 1) print(toys == 1 or toys == 10) print(toys == 1 and toys == 10)

In  The in operator (in) can be used to see whether one string is inside another string (e.g. is a letter in a word / is a word in a sentence) print(‘a’ in ‘abc’) print(‘d’ in ‘abc’) print(‘Mr Crowley’ in ‘Computer Science course with Mr Crowley’) print(‘Mr Hewitt’ in ‘Computer Science course with Mr Crowley’)

Compare Strings  Two strings can be compared using the equal (==) or not equal (!=) operators  Strings have to match exactly to get a True output dog = 'Woof woof' print(dog == 'Woof woof')(True – matches) print(dog == 'woof woof')(False – no capital) print(dog == 'Woof woof ')(False – extra space)

Rachel’s Birthday Today?  Rachel’s birthday is the 9 th July – write a program using logical operators to check if it is Rachel’s birthday today…  Write a second program to check if it is not Rachel’s birthday today  Write a third program to check if it is Rachel’s birthday or New Year or Christmas today import datetime(Python function to find the date) i = datetime.datetime.now()(variable i for time now) %i.day(today’s day) %i.month(today’s month) %s (needed for string formation)

Rachel’s Birthday import datetime i = datetime.datetime.now() day = 9(Rachel’s birthday day) month = 7(Rachel’s birthday month) print((“Rachel’s birthday day is”), day) print((“Rachel’s birthday month is”), month) print(‘Current day is %s’ %i.day) print(‘Current month is %s’ %i.month) print((“Today is Rachel’s birthday:”), day == i.day and month == i.month)

Not Rachel’s Birthday import datetime i = datetime.datetime.now() day = 9 month = 7 print((“Rachel’s birthday day is”), day) print((“Rachel’s birthday month is”), month) print(‘Current day is %s’ %i.day) print(‘Current month is %s’ %i.month) print((“Today is not Rachel’s birthday:”), day != i.day and month != i.month)

Rachel’s Birthday or Xmas or New Year import datetime i = datetime.datetime.now() day = 9 month = 7 Xmas_day = 25 Xmas_month = 12 NY_day = 1 NY_month = 1 print((“Rachel’s birthday day is”), day) print((“Rachel’s birthday month is”), month) print((“Xmas day is”), day) print((“Xmas month is”), month) print((“New Years day is”), day) print((“New Years month is”), month) print(‘Current day is %s’ %i.day) print(‘Current month is %s’ %i.month) print(("Today is either Rachel's birthday, or Xmas or New Year:"), day == i.day and month == i.month \ or Xmas_day == i.day and Xmas_month == i.month or NY_day == i.day and NY_month == i.month) Use this character (\) to code over multiple lines

Rachel’s Birthday or Xmas or New Year