Download presentation
Presentation is loading. Please wait.
1
Teaching London Computing
1.1 Numbers and Strings Margaret Derrington KCL Easter 2014
2
Remember the two PYTHON windows
The shell Where the program runs The IDLE Where you write and edit a program You can also OPEN an existing program From ANY FILE menu It will open in its own IDLE window where you can edit it and click RUN And it will run in the shell window. If you close the SHELL, it will open when you RUN something If you close the IDLE you can open a new one from the SHELL If you close them both, you will have to open Python all over again
3
The Shell The shell is a bit like a calculator
If you write a line of code in there Next to the prompt >>> When you click RETURN/ENTER/SEND It will run and print… there, where it is, in the shell But you cannot write programs here Because every time you click ENTER It runs what you have just typed But one line scripts will run here… So it is useful for testing and checking things If you are writing a complicated program in the IDLE, you can check or test bits of it as you go.
4
So let’s test some stuff in the shell
Type a string in the shell “Hello World” And it will print it back to you Type a sum * or It will give you the answers… So, what happens if you add a string to a number? What happens if you multiply a string by a number? What happens if you add two strings? Or three? Interesting? It’s called concatenation
5
ARITHMETIC Insert numbers for x and y and work out what these OPERATORS do: x + y x – y x * y x ** y x / y x // y -x +y pow(x,y) You can test them all in the python shell…… ……………………..they are one-liners!!! Check what happens with 10 – 5 – 2 10 – (5-2) TWO kinds of division 10/4 10//4 Well THREE actually 10%4 And 2 * how does this compare with a calculator?
6
Indexing and slicing strings
INDEXING returns a numbered character "william"[0] – gives ‘w’ "william"[1] – gives ‘i’ "william"[6] – gives ‘m’ and “william”.index(“i”) gives 1 …the index or position of the first i SLICING gives a range "william"[1:4]– gives ‘ill’ The numbering starts with ZERO!!!!! (not all languages do this, but many do) INcluding character number 1 EXcluding character number 4
7
String Length len() is a function that returns the LENGTH of a string; the number of characters len(“hello world”) gives (including the space) How would you make python return “world? Use slicing and THINK about the numbers You should be able to do this in FOUR different ways!!!!!! Hint:: use len and index
8
String Length len() is a function that returns the LENGTH of a string; the number of characters len(“hello world”) gives (including the space) How would you make python return “world? Use slicing and THINK about the numbers “hello world” [6:10] Or “hello world” [6:len(“hello world”)] Or “hello world” [“hello world”.index(“ ”):10] or “hello world” [“hello world”.index(“ ”): 6:len(“hello world”)]
9
Errors Python has fewer errors than other languages (e.g. Java)
This has both pros and cons Not everything we write makes sense Syntax error: 123abc 1 ! 3 “hello “I can’t understand what you are asking” Wassup??? Wosswrong????
10
Errors Python has fewer errors than other languages (e.g. Java)
This has both pros and cons Not everything we write makes sense Syntax error: 123abc – not a number 1 ! – not an operator “hello – a string with no end “I can’t understand what you are asking”
11
Why do these not make sense???
Evaluation Errors “The text looks ok but when I try to calculate, it makes no sense” 42 + “hello” 42 / 0 “hello”[17] Why do these not make sense??? What’s wrong?
12
Evaluation Errors “The text looks ok but when I try to calculate, it makes no sense” 42 + “hello” – can’t add a number to a string 42 / – can’t divide by zero “hello”[17] – can’t index beyond the end “hello”[5] is this correct ?????
13
We have speeded up and learnt a LOT more!!!
IDLE, SHELL, open save run ARITHMETIC including some new operators ** // and % and a function pow (n,m) Strings: indexing, slicing, concatenating, the function len() and the method “string”.index() And a little bit about errors. We have been doing these in the shell Next we are going to use what we have learnt to write some PROGRAMS…… where?
14
Back in the IDLE window Write a program that will print your name
print the number of letters in your name Slice your name and print it letter by letter each on a different line Add the number on letters in your first name to the number in your second name and print the answer. Save your program in your named file in my documents… call it something suitable. THINK…. Are you going to do this all at once or are you going to try it out bit by bit, adding each next bit when you have got the rest to work? ……… just wondered….
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.