|
1 | | -## Comments :thought_balloon: |
| 1 | +## Indentation |
2 | 2 |
|
3 | | -### Basic of Comments :bell: |
4 | | - |
5 | | -- Comments :thought_balloon: are very important while writing a program. |
6 | | -- It describes what's going on inside a program so that a person looking at the source code does not have a hard time figuring it out. |
7 | 3 |
|
8 | | -For comments there are two options |
| 4 | +### Basic of Indentation |
| 5 | +**indentation** concepts which play a vital role in python app development |
9 | 6 |
|
10 | | -#### 1.Single line comment :skull: |
11 | | - - we use the hash (**#**) symbol to start writing a comment. |
12 | | - - Python Interpreter ignores comment. |
| 7 | +- One of the most distinctive features of Python is its use of indentation to mark blocks of code. |
| 8 | +- Other some languages such as c and c++ uses curly braces(**{}**) to differentiate block of code |
| 9 | +- But in python, we use indentation to differentiate block of code (basically tab of some active space) |
| 10 | +- we use a colon as the starting of code block and afterwords all code block will be placed. |
| 11 | +- Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the indentation level of the line, which in turn is used to determine the grouping of statements. |
| 12 | +- A code block (body of a function, loop, etc.) starts with indentation and ends with the first unindented line. |
| 13 | +- The amount of indentation is up to you, but it must be consistent throughout that block. |
13 | 14 |
|
14 | | - For example: |
15 | | - ```python |
16 | | - #this is first comment |
17 | | - #this is second comment |
18 | | - a=5 #code with comment |
| 15 | +### For example: |
| 16 | +consider if loop |
| 17 | +In C Programing :scroll:: |
| 18 | +```python |
| 19 | +if(conditions) { |
| 20 | + #main code |
| 21 | +} |
19 | 22 | ``` |
| 23 | +In python Programing: |
20 | 24 |
|
21 | | -#### 2.Multi line comment :dolls: |
22 | | - - Use triple quotes, either **'''** or **"""**. For multicommenting. |
23 | | - - These triple quotes are generally used for multi-line strings. |
24 | | - But they can be used as multi-line comment as well. Unless they are not docstrings, |
25 | | - they do not generate any extra code. |
| 25 | +With Brackets |
| 26 | +```python |
| 27 | +if(conditions) : |
| 28 | + #Write Your Main Code Here |
| 29 | +``` |
26 | 30 |
|
27 | | -For Example: |
| 31 | +without Brackets |
28 | 32 | ```python |
29 | | - ''' this is multi line comment |
30 | | - Second line comment |
31 | | - Third line comment |
32 | | - Fourth line ''' |
| 33 | +if conditions: |
| 34 | + #Write Your Main Code Here |
33 | 35 | ``` |
34 | 36 |
|
35 | | -Or |
| 37 | +### How can we write code block in one line |
| 38 | +- Indentation can be ignored in line with continuation. |
| 39 | +- But it's a good idea to always indent. It makes the code more readable. |
| 40 | +Example: |
| 41 | +with Brackets |
36 | 42 | ```python |
37 | | - """this is multi line comment |
38 | | - Second line comment |
39 | | - Third line comment |
40 | | - Fourth line """ |
| 43 | +if(conditions) :#main code |
41 | 44 | ``` |
42 | 45 |
|
| 46 | +Without Brackets |
| 47 | +```python |
| 48 | +if conditions: #Write Your Main Code Here |
| 49 | +``` |
43 | 50 |
|
44 | | -### Is this possible to assign multiple lines :page_with_curl: to a single variable |
| 51 | +### Wrong Spaces Inside Program Create Indentation Error. |
45 | 52 |
|
46 | | -yes you can use in following ways: |
| 53 | +Consider following code block |
47 | 54 | ```python |
48 | | -myvar= """this is multi line comment |
49 | | - Second line comment |
50 | | - Third line comment |
51 | | - Fourth line """ |
52 | | - |
53 | | -#for print result |
54 | | -print(myvar) |
| 55 | +if(1==1) : |
| 56 | + print("First line") |
| 57 | + print("second line") |
55 | 58 | ``` |
56 | 59 |
|
57 | | -**Note:-** multi line comments are also used to create doc string for function or class |
| 60 | +Incorrect indentation will result into |
| 61 | +```python |
| 62 | +IndentationError: unindent does not match any outer indentation level |
| 63 | +``` |
| 64 | + |
| 65 | +Correct Solution |
| 66 | +```python |
| 67 | +if 1==1 : |
| 68 | + print("First line") |
| 69 | + print("second line") |
| 70 | +``` |
| 71 | + |
| 72 | +Output |
| 73 | +``` |
| 74 | +First line |
| 75 | +second line |
| 76 | +``` |
0 commit comments