Python Tutorial ( PDFDrive )
• complex (complex numbers)Examples: Here are some examples of numbers:Int Long float complex10 51924361L 0.0 3.14j100 -0x19323L 15.20 45.j-786 0122L -21.9 9.322e-36j080 0xDEFABCECBDAECBFBAEl 32.3+e18 .876j-0490 535633629843L -90. -.6545+0J-0x260 -052318172735L -32.54e100 3e+26J0x69 -4721885298529L 70.2-E12 4.53e-7j• Python allows you to use a lowercase L with long, but it is recommended that you use only an uppercase L toavoid confusion with the number 1. Python displays long integers with an uppercase L.• A complex number consists of an ordered pair of real floating-point numbers denoted by a + bj, where a is thereal part and b is the imaginary part of the complex number.Python Strings: Strings in Python are identified as a contiguous set of characters in between quotation marks. Python allows foreither pairs of single or double quotes. Subsets of strings can be taken using the slice operator ( [ ] and [ : ] ) withindexes starting at 0 in the beginning of the string and working their way from -1 at the end.The plus ( + ) sign is the string concatenation operator and the asterisk ( * ) is the repetition operator. Forexample:#!/usr/bin/pythonstr = 'Hello World!'print str# Prints complete stringprint str[0] # Prints first character of the stringprint str[2:5] # Prints characters starting from 3rd to 5thprint str[2:] # Prints string starting from 3rd characterprint str * 2 # Prints string two timesprint str + "TEST" # Prints concatenated stringThis will produce the following result:Hello World!Hllollo World!Hello World!Hello World!Hello World!TESTTUTORIALS POINT Simply Easy Learning
Python Lists: Lists are the most versatile of Python's compound data types. A list contains items separated by commas andenclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between themis that all the items belonging to a list can be of different data type.The values stored in a list can be accessed using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in thebeginning of the list and working their way to end -1. The plus ( + ) sign is the list concatenation operator, and theasterisk ( * ) is the repetition operator. For example:#!/usr/bin/pythonlist = [ 'abcd', 786 , 2.23, 'john', 70.2 ]tinylist = [123, 'john']print list# Prints complete listprint list[0] # Prints first element of the listprint list[1:3] # Prints elements starting from 2nd till 3rdprint list[2:] # Prints elements starting from 3rd elementprint tinylist * 2 # Prints list two timesprint list + tinylist # Prints concatenated listsThis will produce the following result:['abcd', 786, 2.23, 'john', 70.200000000000003]abcd[786, 2.23][2.23, 'john', 70.200000000000003][123, 'john', 123, 'john']['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john']Python Tuples: A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separatedby commas. Unlike lists, however, tuples are enclosed within parentheses.The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements andsize can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can bethought of as read-only lists. For example:#!/usr/bin/pythontuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )tinytuple = (123, 'john')print tuple# Prints complete listprint tuple[0] # Prints first element of the listprint tuple[1:3] # Prints elements starting from 2nd till 3rdprint tuple[2:] # Prints elements starting from 3rd elementprint tinytuple * 2 # Prints list two timesprint tuple + tinytuple # Prints concatenated listsThis will produce the following result:('abcd', 786, 2.23, 'john', 70.200000000000003)abcdTUTORIALS POINT Simply Easy Learning
- Page 7 and 8: Return Value ......................
- 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: Here, an integer object is created
- Page 61 and 62: FunctionDescriptionint(x [,base])Co
- 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
• complex (complex numbers)
Examples:
Here are some examples of numbers:
Int Long float complex
10 51924361L 0.0 3.14j
100 -0x19323L 15.20 45.j
-786 0122L -21.9 9.322e-36j
080 0xDEFABCECBDAECBFBAEl 32.3+e18 .876j
-0490 535633629843L -90. -.6545+0J
-0x260 -052318172735L -32.54e100 3e+26J
0x69 -4721885298529L 70.2-E12 4.53e-7j
• Python allows you to use a lowercase L with long, but it is recommended that you use only an uppercase L to
avoid confusion with the number 1. Python displays long integers with an uppercase L.
• A complex number consists of an ordered pair of real floating-point numbers denoted by a + bj, where a is the
real part and b is the imaginary part of the complex number.
Python Strings:
Strings in Python are identified as a contiguous set of characters in between quotation marks. Python allows for
either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ( [ ] and [ : ] ) with
indexes starting at 0 in the beginning of the string and working their way from -1 at the end.
The plus ( + ) sign is the string concatenation operator and the asterisk ( * ) is the repetition operator. For
example:
#!/usr/bin/python
str = 'Hello World!'
print str
# Prints complete string
print str[0] # Prints first character of the string
print str[2:5] # Prints characters starting from 3rd to 5th
print str[2:] # Prints string starting from 3rd character
print str * 2 # Prints string two times
print str + "TEST" # Prints concatenated string
This will produce the following result:
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
TUTORIALS POINT
Simply Easy Learning