Skip to content

Commit 7cffa79

Browse files
authored
Merge pull request #86 from codewithdhruba01/add/Python-Syntax
docs: Add detailed Python Syntax documentation with examples and explanations
2 parents 2ae9583 + 3b562b7 commit 7cffa79

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed

docs/python/python-syntax.md

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
---
2+
id: python-syntax
3+
title: Python Syntax
4+
sidebar_label: Python Syntax #displays in sidebar
5+
sidebar_position: 2
6+
tags:
7+
[
8+
Python,
9+
Introduction of python,
10+
Python Syntax,
11+
12+
]
13+
14+
---
15+
16+
# Python Syntax
17+
18+
Python is known for its clean and readable syntax. It emphasizes code readability and allows developers to write fewer lines of code compared to other programming languages.
19+
20+
### Basic Syntax Structure
21+
22+
Python uses indentation instead of curly braces `{}` to define blocks of code.
23+
24+
### Example:
25+
26+
```python
27+
if 5 > 2:
28+
print("Five is greater than two!")
29+
````
30+
31+
* **Indentation** is crucial in Python. Missing or incorrect indentation will raise an error.
32+
33+
34+
### Comments
35+
36+
### Single-line comment:
37+
38+
```python
39+
# This is a comment
40+
print("Hello, World!")
41+
```
42+
43+
### Multi-line comment (using triple quotes):
44+
45+
```python
46+
"""
47+
This is a
48+
multi-line comment
49+
"""
50+
print("Hello again!")
51+
```
52+
53+
---
54+
55+
### Variables
56+
57+
Python does not require you to declare the type of a variable.
58+
59+
```python
60+
x = 10 # integer
61+
y = "Hello" # string
62+
z = 3.14 # float
63+
```
64+
65+
### Multiple assignment:
66+
67+
```python
68+
a, b, c = 1, 2, 3
69+
```
70+
71+
---
72+
73+
### Data Types
74+
75+
Some common data types in Python:
76+
77+
* `int`: Integer
78+
* `float`: Floating point
79+
* `str`: String
80+
* `bool`: Boolean
81+
* `list`: List of items
82+
* `tuple`: Immutable list
83+
* `dict`: Key-value pair
84+
* `set`: Unique unordered collection
85+
86+
```python
87+
num = 10 # int
88+
name = "Alice" # str
89+
items = [1, 2, 3] # list
90+
person = {"name": "Bob", "age": 25} # dict
91+
```
92+
93+
---
94+
95+
### Conditionals
96+
97+
```python
98+
age = 18
99+
100+
if age >= 18:
101+
print("Adult")
102+
elif age > 12:
103+
print("Teenager")
104+
else:
105+
print("Child")
106+
```
107+
108+
---
109+
110+
### Loops
111+
112+
### `for` loop:
113+
114+
```python
115+
for i in range(5):
116+
print(i)
117+
```
118+
119+
### `while` loop:
120+
121+
```python
122+
count = 0
123+
while count < 5:
124+
print(count)
125+
count += 1
126+
```
127+
128+
---
129+
130+
### Functions
131+
132+
Functions are defined using the `def` keyword.
133+
134+
```python
135+
def greet(name):
136+
print("Hello, " + name)
137+
138+
greet("Alice")
139+
```
140+
141+
### Return statement:
142+
143+
```python
144+
def add(a, b):
145+
return a + b
146+
147+
result = add(2, 3)
148+
print(result) # Output: 5
149+
```
150+
151+
---
152+
153+
### Modules and Imports
154+
155+
You can import built-in or custom modules.
156+
157+
```python
158+
import math
159+
160+
print(math.sqrt(16)) # Output: 4.0
161+
```
162+
163+
---
164+
165+
### Operators
166+
167+
### Arithmetic Operators:
168+
169+
```python
170+
+ - * / // % **
171+
```
172+
173+
### Comparison Operators:
174+
175+
```python
176+
== != > < >= <=
177+
```
178+
179+
### Logical Operators:
180+
181+
```python
182+
and or not
183+
```
184+
185+
---
186+
187+
### Indentation Rules
188+
189+
Python uses **4 spaces** (by convention) for indentation. Do not mix tabs and spaces.
190+
191+
Incorrect:
192+
193+
```python
194+
if True:
195+
print("Hello") # IndentationError
196+
```
197+
198+
Correct:
199+
200+
```python
201+
if True:
202+
print("Hello")
203+
```
204+
205+
---
206+
207+
## Conclusion
208+
209+
Python syntax is simple, readable, and beginner-friendly. With its use of indentation and minimalistic style, it allows you to focus on solving problems rather than worrying about complex syntax rules.
210+
211+
---
212+
213+
> 📌 **Note**: Make sure your Python files have the `.py` extension and you're using Python 3.x version for compatibility with modern features.

0 commit comments

Comments
 (0)