Tuples and strings, Python Programming

Tuples  and strings

Python has two different more list-like data  types  that are very important to understand.A tuple  is a structure that  is like a list, but  is not  mutable. You can create  fresh tuples, but  you cannot modify  the contents of a tuple  or add  components to it. A tuple  is typically given like a list, but with round brackets instead of square ones:

 

>>> a = (1, 2, 3)

In fact, it is the commas and not the parentheses that matter here.  So, you may write

 

>>> a = 1, 2, 3

>>> a

(1, 2, 3)

and  still get a tuple.  The only tricky  thing  about  tuples is making a tuple  with  a one  component. We could try

 

>>> a = (1)

>>> a

1

but  it does  not  work,  because  in the  expression (1) the  parentheses are  playing the  standard grouping role (and,  in fact, because  parentheses do not create  tuples).  So, to create  a tuple  with  a single component, we have to use a comma:

 

>>> a = 1,

>>> a

(1,)

Tuples  will be very important in case  where we are using  structured objects as 'keys', that  is, to index  into another data  structure, and  where inconsistencies would occur if those  keys could  be changed.

An important special  kind  of tuple  is a character.   A string  may almost  be thought of as a tuple  of characters. The information  of what  constitutes a character and how they are encoded is hard, because  modern string sets add characters from nearly  all the world's languages. We will take characters we may type easily on our keyboards. In Python, you may type a string with either  single or double quotes:  'abc' or "abc". You can choose parts  of it as you have a list:

>>> s = 'abc'

>>> s[0]

'a'

>>> s[-1]

'c'

 

The strange thing about this is that s is a string, and because Python has no common data type to show a single character, s[0] is also a string.

 

We will frequently use + to concatenate two existing strings to make a new one:

 

>>> to = 'Jody'

>>> fromP = 'Robin'

>>> letter = 'Dear ' + to + ",\n It's over.\n" + fromP

>>> print letter

Dear Jody,

It's over.

Robin

 

As well as using  + to concatenate strings, this code explains several  other  small but impor­tant points:

 

  • You may put a single quote under a string that is delimited by double-quote characters (and vice versa).
  • If you want a new line in your string, you can write \n. Or, if you delimit your string with a triple quote, it can take over multiple lines.
  • The print statement can be used to print out results in your program.
  • Python, like most other programming languages, has some reserved keywords that have special function and cannot be used as variables. In that type, we needed to use from, but that has a unique meaning to Python, so we needed fromP instead.

 

Posted Date: 8/11/2012 1:52:37 AM | Location : United States







Related Discussions:- Tuples and strings, Assignment Help, Ask Question on Tuples and strings, Get Answer, Expert's Help, Tuples and strings Discussions

Write discussion on Tuples and strings
Your posts are moderated
Related Questions
Procedures as first-class objects In Python, unlike  many  other  languages, methods are behave in much  the same way as num­ bers:  they  can be stored as values  of variable

Procedure calls When you compute an expression of the form ( ,  ..., )   the Python interpreter treats  this as a procedure call. It will be simpler to talk about

Imagine a "particle" located on the centre square of a two-dimensional grid of dimensions 11 by 75. The particle can only move one square at a time, either up, down, left, or right

Tower of Hanoi game that you can let a player to move discs between the towers using a mouse. Moreover, you are required to do the followings: •Graphically represent any state in t

Finally! After years of adding unimportant sports, the IOC has finally added Shoe Tying to the Olympics. And you’ve been selected to write the software to handle the judging of t

Task This problem creates a list containing names. The list of names will be printed, sorted, printed again in the new sorted order, written to a new output file, and searched. Ca

Tuples  and strings Python has two different more list-like data  types  that are very important to understand.A tuple  is a structure that  is like a list, but  is not  mutab

Write an algorithm for a program that receives, as input from the user, 2 string variables and 2 integer variables; then joins together and displays the combined strings; and final

When investing money, an important concept to know is compound interest. The equation FV = PV (1+rate)periods . This relates the following four quantities. The present value (

Import the sample code below into the Python IDLE and enhance it, run it and debug it. Add features to make this a more realistic database tool by providing for easy data entry an