Thursday, February 27, 2020

Python 101

i joined a Harvard Online Introduction course using Python for Research.

it is really good to get to know a new programming language. here is some 101 fact for Python

1. [:]  is for Array slice which is a shadow copy of the array content

for example a=[1 , 2 , 3 , 4]

b=a   #a and b reference to the same object. however
b=a[:]   #a and b have same content. but reference to two different object due to shadow copy of array a.

2. global value vs local variable

  x=1 # which is a global value

   def printx();
     x=2  # x is a local variable which different from the global value above
    print(x) # print function will output 2 since the print function execute inside the printx fucntion.

print(x) # output will be 1 since global x is 1

another example shows that global and local variable do not interference each other

x=1
while x<5:
 x *=2

the final value of x is 4

3. # is the inline comment character, use Ctrl + / to comment out or uncomment highlight rows or multi line delimiter (""")


4. len([]) will only count distinct value only
len[1, 1, 2, 3, 3, 4] =4

5. a=["Hello, World"]
a[5:] will be equal to a.slice(5) which will be result in ", World"

6. the function block is identified the indentation
for example
def myFunc():
      #line 1
      #line 2
      #line 3 the function is end here

#this line is  out of function. same applies to for loop and while loop.
python use indentation for to indicate a block of code

Happy programming. enjoy the fun on learning a new language.





No comments:

Post a Comment