Python Basic Öffentlich

Python Basic

Bhavuk kumar
Kurs von Bhavuk kumar, aktualisiert more than 1 year ago Beitragende

Beschreibung

Learning basic of Python programming language.

Modulinformationen

Keine Merkmale angegeben
Important Notes To Remember :  1. Python code is designed to be readable, and hence reusable and maintainable—much more so than traditional scripting languages.  2. Python comes with a large collection of prebuilt and portable functionality, known as the standard library.  3. Google makes extensive use of Python in its web search systems. 4. The popular YouTube video sharing service is largely written in Python. 5. The widespread BitTorrent peer-to-peer file sharing system began its life as a Python program. 6. Google’s App Engine web development framework uses Python as an application language. 7. Netflix and Yelp have both documented the role of Python in their software infrastructures. 8. Intel, Cisco, Hewlett-Packard, Seagate, Qualcomm, and IBM use Python for hardware testing. 9. JPMorgan Chase, UBS, Getco, and Citadel apply Python to financial market forecasting. 10. CPython is the standard implementation of the language. 11. Jython and IronPython implement Python programs for use in Java and .NET environments, respectively; they are alternative compilers for Python. Interpreter :   1. A software package called an interpreter. An interpreter is a kind of program that executes other programs. 2. When you write a Python program, the Python interpreter reads your program and carries out the instructions it contains. 3. When the Python package is installed on your machine, it generates a number of components—minimally, an interpreter and a support library. Python Installation:  Learn from video lectures (Windows, Mac & Linux) Byte code compilation: when you execute a program Python first compiles your source code (the statements in your file) into a format known as byte code. EX. a .pyc extension (“.pyc” means compiled “.py” source). a.py >> a.pyc >> PVM Source Code >> Byte Code >> RunTime
Weniger sehen
Keine Merkmale angegeben
Important Topics : 1. Variables : Python uses 'Dynamic Typing' , Means you can reassign the variables to different data type. EX. my_dog = 2  // Integer data typed       my_dog = ["Tom" , "John"]   // list data typed 2. Strings :  >>> S = 'Spam'    # Make a 4-character string, and assign it to a name >>> len(S)            # Length 4 >>> S[0]               # The first item in S, indexing by zero-based position 'S' >>> S[1]               # The second item from the left 'p' >>> S[-1]                # The last item from the end in S 'm' >>> S[-2]                # The second-to-last item from the end 'a' >>> S[-1]                # The last item in S 'm' >>> S[len(S)-1]          # Negative indexing, the hard way 'm' >>> S                     # A 4-character string 'Spam' >>> S[1:3]                # Slice of S from offsets 1 through 2 (not 3)  //Gives us all the characters in string S from offsets 1 through 2 (that is, 1 through 3 – 1) as a new string. 'pa' >>> S[1:]                 # Everything past the first (1:len(S)) 'pam' >>> S                     # S itself hasn't changed 'Spam' >>> S[0:3]                # Everything but the last 'Spa' >>> S[:3]                 # Same as S[0:3] 'Spa' >>> S[:-1]                # Everything but the last again, but simpler (0:-1) 'Spa' >>> S[:]                  # All of S as a top-level copy (0:len(S)) 'Spam' >>> S + 'xyz'             # Concatenation 'Spamxyz' >>> S                     # S is unchanged 'Spam' >>> S * 8                 # Repetition 'SpamSpamSpamSpamSpamSpamSpamSpam' >>> S 'Spam' >>> S[0] = 'z'             # Immutable objects cannot be changed ...error text omitted... TypeError: 'str' object does not support item assignment >>> S = 'z' + S[1:]        # But we can run expressions to make new objects >>> S 'zpam' >>> S = 'shrubbery' >>> L = list(S)                                     # Expand to a list: [...] >>> L ['s', 'h', 'r', 'u', 'b', 'b', 'e', 'r', 'y'] >>> L[1] = 'c'                                      # Change it in place >>> ''.join(L)                                      # Join with empty delimiter 'scrubbery' >>> B = bytearray(b'spam')                          # A bytes/list hybrid (ahead) >>> B.extend(b'eggs')                               # 'b' needed in 3.X, not 2.X >>> B                                               # B[i] = ord(c) works here too bytearray(b'spameggs') >>> B.decode()                                      # Translate to normal string 'spameggs' >>> S = 'Spam' >>> S.find('pa')                 # Find the offset of a substring in S 1 >>> S 'Spam' >>> S.replace('pa', 'XYZ')       # Replace occurrences of a string in S with another 'SXYZm' >>> S 'Spam' >>> line = 'aaa,bbb,ccccc,dd' >>> line.split(',')              # Split on a delimiter into a list of substrings ['aaa', 'bbb', 'ccccc', 'dd'] >>> S = 'spam' >>> S.upper()                    # Upper- and lowercase conversions 'SPAM' >>> S.isalpha()                  # Content tests: isalpha, isdigit, etc. True >>> S = 'A\nB\tC'            # \n is end-of-line, \t is tab >>> len(S)                   # Each stands for just one character 5 >>> ord('\n')                # \n is a byte with the binary value 10 in ASCII 10 >>> S = 'A\0B\0C'            # \0, a binary zero byte, does not terminate string >>> len(S) 5
Weniger sehen
Keine Merkmale angegeben
Important Topics Continue: 3. Lists :  - lists of files in a folder, employees in a company, emails in your inbox, and so on. - Python’s lists may be reminiscent of arrays in other languages, but they tend to be more powerful. - Further, lists have no fixed size. That is, they can grow and shrink on demand, in response to list-specific operations: >>> L = [123, 'spam', 1.23]   # A list of three different-type objects >>> len(L)                               # Number of items in the list 3 >>> L[0]                                 # Indexing by position 123 >>> L[:-1]                               # Slicing a list returns a new list [123, 'spam'] >>> L + [4, 5, 6]                      # Concat/repeat make new lists too [123, 'spam', 1.23, 4, 5, 6] >>> L * 2 [123, 'spam', 1.23, 123, 'spam', 1.23] >>> L                                       # We're not changing the original list [123, 'spam', 1.23] >>> L.append('NI')                # Growing: add object at end of list >>> L [123, 'spam', 1.23, 'NI'] >>> L.pop(2)                           # Shrinking: delete an item in the middle 1.23 >>> L                                        # "del L[2]" deletes from a list too [123, 'spam', 'NI'] >>> M = ['bb', 'aa', 'cc'] >>> M.sort() >>> M ['aa', 'bb', 'cc'] >>> M.reverse() >>> M ['cc', 'bb', 'aa']>>> M = ['bb', 'aa', 'cc'] >>> M.sort() >>> M ['aa', 'bb', 'cc'] >>> M.reverse() >>> M ['cc', 'bb', 'aa']
Weniger sehen
Keine Merkmale angegeben
Important Topics Continue: 4. Nesting :    >>> M = [[1, 2, 3],               # A 3 × 3 matrix, as nested lists          [4, 5, 6],               # Code can span lines if bracketed          [7, 8, 9]] >>> M [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Here, we’ve coded a list that contains three other lists. The effect is to represent a 3 × 3 matrix of numbers. Such a structure can be accessed in a variety of ways: >>> M[1]                          # Get row 2 [4, 5, 6] >>> M[1][2]                       # Get row 2, then get item 3 within the row 6 5. Comprehensions : >>> col2 = [row[1] for row in M]             # Collect the items in column 2 >>> col2 [2, 5, 8] >>> M                                        # The matrix is unchanged [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> [row[1] + 1 for row in M]                 # Add 1 to each item in column 2 [3, 6, 9] >>> [row[1] for row in M if row[1] % 2 == 0]  # Filter out odd items [2, 8] >>> diag = [M[i][i] for i in [0, 1, 2]]      # Collect a diagonal from matrix >>> diag [1, 5, 9] >>> doubles = [c * 2 for c in 'spam']        # Repeat characters in a string >>> doubles ['ss', 'pp', 'aa', 'mm'] >>> list(range(4))                           # 0..3 (list() required in 3.X) [0, 1, 2, 3] >>> list(range(−6, 7, 2))                    # −6 to +6 by 2 (need list() in 3.X) [−6, −4, −2, 0, 2, 4, 6] >>> [[x ** 2, x ** 3] for x in range(4)]     # Multiple values, "if" filters [[0, 0], [1, 1], [4, 8], [9, 27]] >>> [[x, x / 2, x * 2] for x in range(−6, 7, 2) if x > 0] [[2, 1, 4], [4, 2, 8], [6, 3, 12]] >>> G = (sum(row) for row in M)              # Create a generator of row sums >>> next(G)                                  # iter(G) not required here 6 >>> next(G)                                  # Run the iteration protocol next() 15 >>> next(G) 24 >>> list(map(sum, M))                        # Map sum over items in M [6, 15, 24] In Python 2.7 and 3.X, comprehension syntax can also be used to create sets and dictionaries: >>> {sum(row) for row in M}                  # Create a set of row sums {24, 6, 15} >>> {i : sum(M[i]) for i in range(3)}        # Creates key/value table of row sums {0: 6, 1: 15, 2: 24} In fact, lists, sets, dictionaries, and generators can all be built with comprehensions in 3.X and 2.7: >>> [ord(x) for x in 'spaam']                # List of character ordinals [115, 112, 97, 97, 109] >>> {ord(x) for x in 'spaam'}                # Sets remove duplicates {112, 97, 115, 109} >>> {x: ord(x) for x in 'spaam'}             # Dictionary keys are unique {'p': 112, 'a': 97, 's': 115, 'm': 109} >>> (ord(x) for x in 'spaam')                # Generator of values <generator object <genexpr> at 0x000000000254DAB0>
Weniger sehen
Keine Merkmale angegeben
Important Topics Continue: 6. Dictionaries : Python dictionaries are something completely different —they are not sequences at all, but are instead known as mappings. Mappings are also collections of other objects, but they store objects by key instead of by relative position. Dictionaries are coded in curly braces and consist of a series of “key: value” pairs. Dictionaries are useful anytime we need to associate a set of values with keys—to describe the properties of something, As an example, consider the following three-item dictionary (with keys “food,” “quantity,” and “color,” perhaps the details of a hypothetical menu item?): >>> D = {'food': 'Spam', 'quantity': 4, 'color': 'pink'} >>> D['food']              # Fetch value of key 'food' 'Spam' >>> D['quantity'] += 1     # Add 1 to 'quantity' value >>> D {'color': 'pink', 'food': 'Spam', 'quantity': 5} >>> D = {} >>> D['name'] = 'Bob'      # Create keys by assignment >>> D['job']  = 'dev' >>> D['age']  = 40 >>> D {'age': 40, 'job': 'dev', 'name': 'Bob'} >>> print(D['name']) Bob We can also make dictionaries by passing to the dict type name either keyword arguments (a special name=value syntax in function calls) >>> bob1 = dict(name='Bob', job='dev', age=40)                       >>> bob1 {'age': 40, 'name': 'Bob', 'job': 'dev'} >>> bob2 = dict(zip(['name', 'job', 'age'], ['Bob', 'dev', 40]))    # Zipping >>> bob2 {'job': 'dev', 'name': 'Bob', 'age': 40}
Weniger sehen
Zusammenfassung anzeigen Zusammenfassung ausblenden