Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Python Expert

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.

 

Python, Programming

  • Category:- Python
  • Reference No.:- M9506027

Have any Question?


Related Questions in Python

Part i the assignment filesone of the most important

Part I: The Assignment Files One of the most important outcomes of this assignment is that you understand the importance of testing. This assignment will follow an iterative development cycle. That means you will write a ...

Sieve of eratosthenes in pythonthe goal is to find all the

Sieve of Eratosthenes (in Python) The goal is to find all the prime numbers less than or equal to some natural number maxn. We have a list that tells us if any of the numbers 0..maxn are "marked". It can be an array of b ...

Foundations of programming assignment - feduni bankingthis

Foundations of Programming Assignment - FedUni Banking This assignment will test your skills in designing and programming applications to specification. Assignment Overview - You are tasked with creating an application t ...

The second task in this assignment is to create a python

The second task in this assignment is to create a Python program called pancakes.py that will determine the final order of a stack of pancakes after a series of flips.(PYTHON 3) Problem Task In this problem, your input w ...

Quesiton write a python script that counts occurrences of

Quesiton: Write a python script that counts occurrences of words in a file. • The script expects two command-line arguments: the name of an input file and a threshold (an integer). Here is an example of how to run the sc ...

Question research pythons dictionary data type dictdiscuss

Question : Research Python's dictionary data type (dict). Discuss its interface and usage. Include examples. Discuss practical applications of dictionaries.

Question write a simple python program that takes use

Question: Write a simple python program that takes use inputs as non-zero digits and converts them into binary form. The response must be typed, single spaced, must be in times new roman font (size 12) and must follow th ...

Architecture and system integrationcase study queensland

Architecture and System Integration Case Study: Queensland Health - eHealth Investment Strategy After evaluating various platforms, Queensland Health finally decided to adopt a Service Oriented Architecture (SOA) for its ...

A software company sells a package that retails for 99

A software company sells a package that retails for $99. Quantity discounts are given according to the following table: Quantity Discount 10 - 19 20% 20 - 49 30% 50 - 99 40% 100 or more 50% Write a program using python t ...

Tasksdemonstrate data scraping of a social network of

Tasks Demonstrate data scraping of a social network of choice. Develop technical documentation, including the development of the code & detailing the results. Provide a report on the findings, that includes research into ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As