Jump to content

Introduction, question slicing.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Voiden

Voiden

    Newbie

  • Members
  • PipPip
  • 14 posts
Greetings,

I started yesterday with Python, as it is my first (programming) language it seemed to be a logical choice after doing some research. Nonetheless i started with a book called "Python Programming for the Absolute Beginner", and its all going great, atleast in my opinion. But, (a minor but) i do seem to struggle (obviously) by associating the functions of the programming i'm learning on to implementing it.

PS: It could be because my mind is clogged with all this new information

Anyway, this was the script/program that just didn't make sense to me allthough i understand its function.

# Pizza Slicer

# Demonstrates string slicing

# Xxxxx Xxxxxxxx - 19/10/10


word = "pizza"


print \

"""

   Slicing 'Cheat Sheet'

0     1     2     3     4     5

+-----+-----+-----+-----+-----+

|  P  |  I  |  Z  |  Z  |  A  |

+-----+-----+-----+-----+-----+

-5   -4    -3    -2    -1


"""


print "Enter the beginning and ending index for your slice of 'pizza'."

print "Press the enter key at 'Begin' to exit."



begin = None

while begin != "":

    begin = (raw_input("\nBegin: "))


    if begin:

        begin = int(begin)


        end = int(raw_input("End: "))


        print "word[", begin, ":", end, "]\t\t",

        print word[begin:end]


raw_input("\n\nPress the enter key to exit.")

Do i want to slice because strings are immutable? and therefore slicing is one of the alternatives?
Also, could i be explained when and why i want to slice. Or is this simply a matter of time before i'd understand when and why?

:confused:

#2
Voiden

Voiden

    Newbie

  • Members
  • PipPip
  • 14 posts
I guess i was greedy for more knowledge :-P


# Hero's Inventory 2.0

# Demonstrates tuples

# Xxxxx Xxxxxxxx - 19/10/10


# create a tuple with some items and display with a for loop

inventory = ("sword", "armor", "shield", "healing potion")

print "Your items:"

for item in inventory:

    print item


raw_input("\nPress the enter key to continue.")


# get the length of a tuple

print "You have", len(inventory), "items in your possesion."


raw_input("\nPress the enter key to continue.")


# test for membership with in

if "healing potion" in inventory:

    print "You will live to fight another day."


# display one item through an index

index = int(raw_input("\nEnter the index number for an item in inventory: "))

print "At index", index, "is", inventory[index]


# display a slice

begin = int(raw_input("\nEnter the index number to begin a slice: "))

end = int(raw_input("Enter the index number to end the slice: "))

print "inventory[", begin, ":", end, "]\t\t",

print inventory[begin:end]


raw_input("\nPress the enter key to continue.")


# Slice Index

#

#0        1       2        3                4

#+--------+-------+--------+----------------+          

#| "sword"|"armor"|"shield"|"healing potion"|

#+--------+-------+--------+----------------+

#-4      -3      -2       -1


# concatenate two tuples

chest = ("gold", "gems")

print "You find a chest. It contains:"

print chest

print "You add the contents of the chest to your inventory."

inventory += chest

print "Your inventory is now:"

print inventory


raw_input("\n\nPress the enter key to exit.")



#3
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Slicing is one of the great features of Python. Typical reason to slice is to extract substring from string, but it can do much more, for example this one-line script will revert your string:


"Revert me"[::-1] # output: em trever


Another good reason is copying lists. Check this example:


>>> a = [1, 2, 3]

>>> b = a # a and b are the same one list, if you change a then b will be changed either

>>> a is b

True

>>> b = a[:] # now b is new list with elements copied from a

>>> a is b

False