Tuples and strings, Python Programming

Assignment Help:

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.

 


Related Discussions:- Tuples and strings

Common Vulnerabilities, 1 Low Level Exploits 1.1 Savegames Jimmy is becomi...

1 Low Level Exploits 1.1 Savegames Jimmy is becoming increasingly frustrated at the computer game hes playing. He has a save right before the levels boss but he needs either more

corse information, write a program that creates s dictionary containing co...

write a program that creates s dictionary containing course

List Assignment, Task This problem creates a list containing names. The li...

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

Robotics, how to started robotics proggraming in begning

how to started robotics proggraming in begning

Python implementation of a solver for the desert crossing, Assume you have ...

Assume you have a truck which has to travel across a desert from the base camp at position 0 (left) to the target camp at position 4 (right). The intermediate positions 1,2, and 3

Driven rlc circuit and damped tlc circuit, Write a program on python to gi...

Write a program on python to give solution for driven and damped rlc circuit

Program to calculate area function, Rewrite the area.py program (shown bel...

Rewrite the area.py program (shown below, or in the Creating Functions section of the tutorial) so that it has separate functions for the perimeter and area of a square, a rectangl

Otway rees protocol implementation, I need server, client and trusted side ...

I need server, client and trusted side communicating and charging a service with that protocol (or needham shroeder)

Python Program Help, Write a Python program to accomplish the following. U...

Write a Python program to accomplish the following. Use modular design. Include at least 3 functions: one that returns zero values, one that returns one value, and one that retu

Algorithm, given number isprime number or not

given number isprime number or not

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd