Learning python through pym 10-08-2019
Day 1
Installing python
one can install python using package manager depending upon the operating system you are using. As I have a windows machine and ubuntu using linux on windows subsystem. I can install python from https://python.org. And Ubuntu I can use below command to install python.
|
|
Running python
To run python you would call python3 which open Python interpreter. On that you can type print(“Hello World”) to run your first python code which would print Hello World in next line.
To run python code from a file I would need to below text into a file, save suppose as hello.py
The file “hello.py” needs to be executable to be run the code. If file is not executable we can do it by chmod +x hello.py
then we can run file by calling it as ./hello.py
White space and indentation
Whitespace and indentation should be perfect for python. If a line is not correctly indented python interpreter would Indentation error. There are few basic rule to follow,
- Use 4 spaces for indentation
- Never mix tab and spaces
- One line blank after functions
- two lines blank after classes.
- add a space after “,” in dicts, lists, tuples and argument list and after “:” in dicts
- Spaces around assignments and comparers
- no spaces after starting and before ending () brackets/parentheses
Comments
In python on can write comments starting with ‘#’ Still gonna check whether to use ’’’ for multiline comments or not. links:
- https://pymbook.readthedocs.io/en/latest/thebeginning.html#comments
- https://stackoverflow.com/questions/7696924/way-to-create-multiline-comments-in-python
- https://twitter.com/gvanrossum/status/112670605505077248
Modules
Modules are link library which you can in import and run function from it.
Keywords and Indentifier
As any other language there are reserved keywords in python which cannot be used as ordinary identifiers. Below is the list of identifiers. Note: Identifiers are case sensitive they should be used with same casing.
We don’t need specify data type with python something similar to javascript where we use var for every type of variable.