Python Tutorial ( PDFDrive )
print "List : ", aListLet us compile and run the above program, this will produce the following result:List : [123, 'abc', 'xyz', 'xyz', 'zara']TUTORIALS POINT Simply Easy Learning
CHAPTER11Python TuplesAtuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The onlydifference is that tuples can't be changed i.e., tuples are immutable and tuples use parentheses and lists usesquare brackets.Creating a tuple is as simple as putting different comma-separated values and optionally you can put thesecomma-separated values between parentheses also. For example:tup1 = ('physics', 'chemistry', 1997, 2000);tup2 = (1, 2, 3, 4, 5 );tup3 = "a", "b", "c", "d";The empty tuple is written as two parentheses containing nothing:tup1 = ();To write a tuple containing a single value you have to include a comma, even though there is only one value:tup1 = (50,);Like string indices, tuple indices start at 0, and tuples can be sliced, concatenated and so on.Accessing Values in Tuples: To access values in tuple, use the square brackets for slicing along with the index or indices to obtain valueavailable at that index. Following is a simple example:#!/usr/bin/pythontup1 = ('physics', 'chemistry', 1997, 2000);tup2 = (1, 2, 3, 4, 5, 6, 7 );print "tup1[0]: ", tup1[0]print "tup2[1:5]: ", tup2[1:5]When the above code is executed, it produces the following result:tup1[0]: physicstup2[1:5]: [2, 3, 4, 5]TUTORIALS POINT Simply Easy Learning
- Page 119 and 120: import mathprint "degrees(3) : ", m
- Page 121 and 122: CHAPTER9Python StringsStrings are a
- Page 123 and 124: + Concatenation - Adds values on ei
- Page 125 and 126: TAB ( \t ) and they will show up th
- Page 127 and 128: 14151617181920212223242526272829303
- Page 129 and 130: str = "this is string example....wo
- Page 131 and 132: Return Value This method returns a
- Page 133 and 134: Description The method expandtabs()
- Page 135 and 136: Traceback (most recent call last):F
- Page 137 and 138: Let us compile and run the above pr
- Page 139 and 140: str = "This is string example....wo
- Page 141 and 142: str = "-";seq = ("a", "b", "c"); #
- Page 143 and 144: lstrip() Description The method lst
- Page 145 and 146: Max character: xmin(str) Descriptio
- Page 147 and 148: str = "this is really a string exam
- Page 149 and 150: Parameters • chars -- You can sup
- Page 151 and 152: Parameters• str -- This is the st
- Page 153 and 154: Parameters • NAReturn Value This
- Page 155 and 156: Example The following example shows
- Page 157 and 158: CHAPTER10Python ListsThe most basic
- Page 159 and 160: Python Expression Results Descripti
- Page 161 and 162: Syntax Following is the syntax for
- Page 163 and 164: list( seq )Parameters • seq -- Th
- Page 165 and 166: Return Value If elements are of th
- Page 167 and 168: list.insert(index, obj)Parameters
- Page 169: Syntax Following is the syntax for
- Page 173 and 174: len((1, 2, 3)) 3 Length(1, 2, 3) +
- Page 175 and 176: -11-1len(tuple) Description The met
- Page 177 and 178: min value element : 200tuple(seq) D
- Page 179 and 180: #!/usr/bin/pythondict = {'Name': 'Z
- Page 181 and 182: 34str(dict)Produces a printable str
- Page 183 and 184: type(variable) Description The meth
- Page 185 and 186: Syntax Following is the syntax for
- Page 187 and 188: Syntax Following is the syntax for
- Page 189 and 190: Return Value This method does not
- Page 191 and 192: CHAPTER13Python Date & TimeApython
- Page 193 and 194: #!/usr/bin/pythonimport time;localt
- Page 195 and 196: import timeprint "time.altzone %d "
- Page 197 and 198: Parameters • sec -- These are the
- Page 199 and 200: Example The following example shows
- Page 201 and 202: • %w - day of the week as a decim
- Page 203 and 204: time.time( ) Description The metho
- Page 205 and 206: 2time.tznameAttribute time.tzname i
- Page 207 and 208: CHAPTER14Python FunctionAfunction i
- Page 209 and 210: #!/usr/bin/python# Function definit
- Page 211 and 212: return;# Now you can call printinfo
- Page 213 and 214: Inside the function : 30Outside the
- Page 215 and 216: A module is loaded only once, regar
- Page 217 and 218: 'sqrt', 'tan', 'tanh']Here, the spe
- Page 219 and 220: CHAPTER16Python Files I/OThis chapt
CHAPTER
11
Python Tuples
Atuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The only
difference is that tuples can't be changed i.e., tuples are immutable and tuples use parentheses and lists use
square brackets.
Creating a tuple is as simple as putting different comma-separated values and optionally you can put these
comma-separated values between parentheses also. For example:
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";
The empty tuple is written as two parentheses containing nothing:
tup1 = ();
To write a tuple containing a single value you have to include a comma, even though there is only one value:
tup1 = (50,);
Like string indices, tuple indices start at 0, and tuples can be sliced, concatenated and so on.
Accessing Values in Tuples:
To access values in tuple, use the square brackets for slicing along with the index or indices to obtain value
available at that index. Following is a simple example:
#!/usr/bin/python
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5, 6, 7 );
print "tup1[0]: ", tup1[0]
print "tup2[1:5]: ", tup2[1:5]
When the above code is executed, it produces the following result:
tup1[0]: physics
tup2[1:5]: [2, 3, 4, 5]
TUTORIALS POINT
Simply Easy Learning