1 / 14

1.1 Numbers and Strings

T eaching L ondon C omputing. 1.1 Numbers and Strings. Margaret Derrington KCL Easter 2014. 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

callum-levy
Download Presentation

1.1 Numbers and Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TeachingLondon 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 3*85 or 3.8745 - 2.999 • 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 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 * 3 + 5 how does this compare with a calculator? 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!!!

  6. Indexing and slicing strings The numbering starts with ZERO!!!!! (not all languages do this, but many do) • 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’ 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 10 (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 10 (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 “I can’t understand what you are asking” Wassup??? Wosswrong???? • 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

  10. Errors “I can’t understand what you are asking” • 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 ! 3 – not an operator • “hello – a string with no end

  11. Evaluation Errors Why do these not make sense??? What’s wrong? • “The text looks ok but when I try to calculate, it makes no sense” • 42 + “hello” • 42 / 0 • “hello”[17]

  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 / 0 – 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…. 

More Related