Thread: Starting Python
View Single Post
Join Date: Oct 2004
Posts: 4,047
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 935
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting Python

 
0
  #4
Mar 23rd, 2005
When you write a Python program like ...
  1. # use slicing to spell a string in reverse
  2.  
  3. str1 = "Winners never quit, quitters never win!"
  4. # slicing uses [begin : end : step]
  5. # end is exclusive
  6. # defaults are begin = 0, end = len of string, step = 1
  7. # use step = -1 to step from end
  8. print( "reverse = ", str1[::-1] )
... you can save it for instance as 'reverse.py' to a folder. If Python is installed on a Windows computer, you can simply double-click on the filename and run the program. The extension .py is usually associated with the Python interpreter.

Since this is a console program, you will run into the old troublemaker of the console output, the program runs but closes quickly. You have to add a line of code at the end of the program to make the console wait for some key input.

  1. # use slicing to spell a string in reverse
  2.  
  3. str1 = "Winners never quit, quitters never win!"
  4. # slicing uses [begin : end : step]
  5. # end is exclusive
  6. # defaults are begin = 0, end = len of string, step = 1
  7. # use step = -1 to step from end
  8. print( "reverse = ", str1[::-1] )
  9.  
  10. # optional wait for keypress
  11. raw_input('Press Enter...')
  12. # with Python3 'raw_input()' has become 'input()'
This is not needed, if you run from an IDE that has its own output window. BTW, if you are lost with the slicing thing str1[::-1] just fill in the defaults and it gets clearer
str1[0 : len(str1) : -1] .

Just a few more slicing examples ...
  1. # slicing uses [start:<end:step]
  2.  
  3. s4 = "hippopotamus"
  4.  
  5. print( "first 2 char = ", s4[0:2] )
  6. print( "next 2 char = ", s4[2:4] )
  7. print( "last 2 char = ", s4[-2:] )
  8. print( "exclude first 3 char = ", s4[3: ] )
  9. print( "exclude last 4 char = ", s4[:-4] )
  10. print( "reverse the string = ", s4[::-1] ) # step is -1
  11. print( "the whole word again = ", s4 )
  12. print( "spell skipping 2 char = ", s4[::2] ) # step is 2
  13. """
  14. my output -->
  15. first 2 char = hi
  16. next 2 char = pp
  17. last 2 char = us
  18. exclude first 3 char = popotamus
  19. exclude last 4 char = hippopot
  20. reverse the string = sumatopoppih
  21. the whole word again = hippopotamus
  22. spell skipping 2 char = hpooau
  23. """
You can apply slicing to any indexed sequence, here is a list example ...
  1. # exploring Python's slicing operator
  2. # can be used with any indexed sequence like strings, lists, ...
  3. # syntax --> seq[begin : end : step]
  4. # step is optional
  5. # defaults are index begin=0, index end=len(seq)-1, step=1
  6. # -begin or -end --> count from the end backwards
  7. # step = -1 reverses sequence
  8. # if you feel lost, put in the defaults in your mind
  9.  
  10. # use a list as a test sequence
  11. a = [0, 1, 2, 3, 4, 5, 6, 7, 8]
  12.  
  13. print( a[3:6] ) # [3,4,5]
  14. # if either index is omitted, beginning or end of sequence is assumed
  15. print( a[:3] ) # [0,1,2]
  16. print( a[5:] ) # [5,6,7,8]
  17.  
  18. # negative index is taken from the end of the sequence
  19. print( a[2:-2] ) # [2,3,4,5,6]
  20. print( a[-4:] ) # [5,6,7,8]
  21.  
  22. # extract every second element
  23. print( a[::2] ) # [0, 2, 4, 6, 8]
  24.  
  25. # step=-1 will reverse the sequence
  26. print( a[::-1] ) # [8, 7, 6, 5, 4, 3, 2, 1, 0]
  27.  
  28. # no indices just makes a copy (which is sometimes useful)
  29. b = a[:]
  30. print( b ) # [0, 1, 2, 3, 4, 5, 6, 7, 8]
  31.  
  32. # slice in (replace) an element at index 3
  33. b[3:4] = [100]
  34. print( b ) # [0, 1, 2, 100, 4, 5, 6, 7, 8]
  35. # make another copy, since b has changed
  36. b = a[:]
  37.  
  38. # slice in (insert) a few elements starting at index 3
  39. b[3:] = [9, 9, 9, 9] + b[3:]
  40. print( a ) # [0, 1, 2, 3, 4, 5, 6, 7, 8]
  41. print( b ) # [0, 1, 2, 9, 9, 9, 9, 3, 4, 5, 6, 7, 8]
Last edited by vegaseat; 31 Days Ago at 3:05 pm. Reason: slicing examples
May 'the Google' be with you!
Reply With Quote