My Python Notes

Everything is object, including class
Everything has namespace
Everything can be printed / retrospected
Functions are first class object

In Python values carries types
In Python variables do not carry types

Variables means refernce to object
Always object reference is passed to function

PyPy : Run time environment

Python supports
* Multiple inheritence
* mixins
* extensive introspection of type and class
* operator overloading with special member function
* complex numbers
* limited support to lambda


Module = Statements + Definitions
Few important modules are: 
OS : System calls. enviorn for env variables like $HOME. it is dictionary.
sys : to get argv. e.g. sys.argv[1:] because argv[0] is programe name
string, regex, collections, heapq, array, enum, etc. 
numbers, decimals and  fractions : For extra numeric types

Type conversion:
Moderatly type checked language
Implicit conversion for numeric types
No implicit conversion between string and number

Switch = if ... elif ... elif ... else

* Base Types
String is immutable. So replacing a char, returns new String object. 

* Collection (container) Types

Objects can be easily refernced or indexed

A. Sequence : ordered type

1. String = immutable 

String Format

print("I just printed %s pages to the printer %s" % (num, printer))
print("I just printed {0} pages to the printer {1}".format(num, printer))
print("I just printed {num} pages to the printer {printer}".format(num=num, printer=printer))
print(f"I just printed {num} pages to the printer {printer}")

""" OR ''' are used for multiline string

space, coma and newline are default delimiter used in split
x = input("Prompt message"). here x will be always string. 

2. Lists = mutable, dynamic array. 

List has in-build functions like : sort, len, append(one member), remove (value is argument), To delete entry from list at specific index, use : "del list[index]". insert, extend (list), index, pop = pop(-1) (index is argument), no push function.   
List declaration: a_list = [1, 2, "asdf"] OR a_list = list()
comprehension is like lembda function.
primes = [2] 
e.g. list2 = [x ** 2 for x in list1]
Here primes is list with member 2

3. Tuples = immutable.

Tuple declaration: a_tuple = 1, 2, "asdf" OR a_tuple = (1, 2, "asdf")
Tuples are also good for storing closely related data. For example, (x, y, z) coordinates or (r, g, b) colour components

B. Mapping : un-ordered type

1. Dictionaries = Associative Array. Keys are immutable. So String and Tuples can be keys.
Dictionary declaration: dict = {k1:v1, k2:v2}
Useful for JSON objects. 
Dictionary has built-in functions like : has_key(key), get(key, 'optional unknown'), keys, values, del dict['key'], update function will merge the value part: if key found then change value else add the pair. 

one can get list containing keys from dictionary. a_list = list(a_dict)

2. Set : no duplicate. no order, 
Set declaration : a_set = {0, (), "asdf", False}
Set has built-in functions like  : union |, intersection &, difference, symmetric difference -, union - intersection = ^, and subset testing

x = {} This is empty dic not empty set. 

3. Frozenset : immutable set

To iterate through all collection, one can use: 
for item in
The built-in function type() allows us to obtain the type of any object.

Python code compilation to : 

1. CPython : code -> Python byte code
2. Jython : code -> Java Byte Code
3. IronPython : code -> IL(.NET) Byte code

Variable scope

1. local scope
2. global scope
3. built-in scope

* string, number, sequence, map all can be compared
* a < b < c = (a < b) && (b < c)

The core built-in types for manipulating binary data are bytes and bytearray. They are supported by memoryview, which uses the buffer protocol to access the memory of other binary objects without needing to make a copy.



Manish Panchmatia said...

Manish Panchmatia said...

Post a Comment