Python Tutorial ( PDFDrive )
(786, 2.23)(2.23, 'john', 70.200000000000003)(123, 'john', 123, 'john')('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john')Following is invalid with tuple, because we attempted to update a tuple, which is not allowed. Similar case ispossible with lists:#!/usr/bin/pythontuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]tuple[2] = 1000 # Invalid syntax with tuplelist[2] = 1000 # Valid syntax with listPython Dictionary: Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl andconsist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings.Values, on the other hand, can be any arbitrary Python object.Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and accessed using square braces ([] ). For example:#!/usr/bin/pythondict = {}dict['one'] = "This is one"dict[2] = "This is two"tinydict = {'name': 'john','code':6734, 'dept': 'sales'}print dict['one'] # Prints value for 'one' keyprint dict[2]# Prints value for 2 keyprint tinydict# Prints complete dictionaryprint tinydict.keys() # Prints all the keysprint tinydict.values() # Prints all the valuesThis will produce the following result:This is oneThis is two{'dept': 'sales', 'code': 6734, 'name': 'john'}['dept', 'code', 'name']['sales', 6734, 'john']Dictionaries have no concept of order among elements. It is incorrect to say that the elements are "out of order";they are simply unordered.Data Type Conversion: Sometimes, you may need to perform conversions between the built-in types. To convert between types, yousimply use the type name as a function.There are several built-in functions to perform conversion from one data type to another. These functions return anew object representing the converted value.TUTORIALS POINT Simply Easy Learning
FunctionDescriptionint(x [,base])Converts x to an integer. base specifies the base if x is a string.long(x [,base] )Converts x to a long integer. base specifies the base if x is a string.float(x)Converts x to a floating-point number.complex(real [,imag])Creates a complex number.str(x)Converts object x to a string representation.repr(x)Converts object x to an expression string.eval(str)Evaluates a string and returns an object.tuple(s)Converts s to a tuple.list(s)Converts s to a list.set(s)Converts s to a set.dict(d)Creates a dictionary. d must be a sequence of (key,value) tuples.frozenset(s)Converts s to a frozen set.chr(x)Converts an integer to a character.unichr(x)Converts an integer to a Unicode character.TUTORIALS POINT Simply Easy Learning
- Page 9 and 10: Description .......................
- Page 11 and 12: Syntax ............................
- Page 13 and 14: Example ...........................
- Page 15 and 16: Parameters ........................
- Page 17 and 18: Return Value ......................
- Page 19 and 20: Parameters ........................
- Page 21 and 22: Parameters ........................
- Page 23 and 24: Locating Modules: .................
- Page 25 and 26: Parameters ........................
- Page 27 and 28: Description .......................
- Page 29 and 30: Return Value ......................
- Page 31 and 32: Example ...........................
- Page 33 and 34: Parameters ........................
- Page 35 and 36: Regular-expression Modifiers - Opti
- Page 37 and 38: Starting a New Thread: ............
- Page 39 and 40: Example: ..........................
- Page 41 and 42: CHAPTER1Python OverviewPython is a
- Page 43 and 44: CHAPTER2Python EnvironmentBefore we
- Page 45 and 46: • To use this installer python-XY
- Page 47 and 48: -c cmd run Python script sent in as
- Page 49 and 50: This will produce the following res
- Page 51 and 52: file_name = raw_input("Enter filena
- Page 53 and 54: $ python -husage: python [option] .
- Page 55 and 56: print 'Input file is "', inputfilep
- Page 57 and 58: Here, an integer object is created
- Page 59: Python Lists: Lists are the most v
- Page 63 and 64: CHAPTER5Python Basic OperatorsWhat
- Page 65 and 66: Line 4 - Value of c is 2Line 5 - Va
- Page 67 and 68: Line 5 - a is greater than bLine 6
- Page 69 and 70: | Binary OR Operator copies a bit i
- Page 71 and 72: else:print "Line 3 - a and b are tr
- Page 73 and 74: if ( id(a) == id(b) ):print "Line 2
- Page 75 and 76: When you execute the above program,
- Page 77 and 78: if statementsif...else statementsne
- Page 79 and 80: Example: #!/usr/bin/pythonvar1 = 10
- Page 81 and 82: else:print "Could not find true exp
- Page 83 and 84: Loop Typewhile loopfor loopDescript
- Page 85 and 86: num = raw_input("Enter a number :")
- Page 87 and 88: Good bye!Iterating by Sequence I
- Page 89 and 90: 11 is prime13 is prime17 is prime19
- Page 91 and 92: continue statement The continue st
- Page 93 and 94: CHAPTER8Python NumbersNumber data t
- Page 95 and 96: max(x1, x2,...)min(x1, x2,...)modf(
- Page 97 and 98: Syntax Following is the syntax for
- Page 99 and 100: import math# This will import math
- Page 101 and 102: import mathmath.log10( x )Note: Thi
- Page 103 and 104: modf(x) Description The method modf
- Page 105 and 106: round( x [, n] )Parameters • x --
- Page 107 and 108: Return Value This method returns a
- Page 109 and 110: seed([x]) Description The method se
(786, 2.23)
(2.23, 'john', 70.200000000000003)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john')
Following is invalid with tuple, because we attempted to update a tuple, which is not allowed. Similar case is
possible with lists:
#!/usr/bin/python
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tuple[2] = 1000 # Invalid syntax with tuple
list[2] = 1000 # Valid syntax with list
Python Dictionary:
Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and
consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings.
Values, on the other hand, can be any arbitrary Python object.
Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and accessed using square braces (
[] ). For example:
#!/usr/bin/python
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one'] # Prints value for 'one' key
print dict[2]
# Prints value for 2 key
print tinydict
# Prints complete dictionary
print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values
This will produce the following result:
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john']
Dictionaries have no concept of order among elements. It is incorrect to say that the elements are "out of order";
they are simply unordered.
Data Type Conversion:
Sometimes, you may need to perform conversions between the built-in types. To convert between types, you
simply use the type name as a function.
There are several built-in functions to perform conversion from one data type to another. These functions return a
new object representing the converted value.
TUTORIALS POINT
Simply Easy Learning