Skip to content

Commit 91052b1

Browse files
authored
Merge pull request #2 from gauravfs-14/main
[Add[: CodeStructure, VariableLiterals, PrintFunction
2 parents a762329 + 165328e commit 91052b1

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

docs/python-basics/print-function.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
sidebar_position: 4
3+
---
4+
5+
# Print Function
6+
7+
The print function in Python is used to display messages on the console. You can pass any value to it, and it will convert it to a string and print it. If you want to print multiple items, you can separate them with commas. By default, there is no separator, but you can add one using the sep argument. Additionally, each print statement ends with a new line, but you can change this using the end argument.
8+
9+
## Examples
10+
11+
### Basic Print:
12+
13+
```python
14+
print("Welcome to Python!")
15+
```
16+
17+
### Multiple Elements with Default Separator:
18+
19+
```python
20+
print("Python", "is", "fun")
21+
```
22+
23+
Output: Pythonisfun
24+
25+
### Using a Custom Separator:
26+
27+
```python
28+
print("Python", "is", "fun", sep=" ")
29+
```
30+
31+
Output: Python is fun
32+
33+
### Changing the End Character:
34+
35+
```python
36+
print("Hello", end="-")
37+
print("World")
38+
```
39+
40+
Output: Hello-World
41+
42+
### Combining Both sep and end:
43+
44+
```python
45+
print("Learn", "Python", sep="\*", end="!")
46+
print("It's great")
47+
```
48+
49+
Output: Learn\*Python!It's great
50+
51+
These examples should help you understand how to use the print function effectively.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# Python Code Structure
6+
7+
## Maintainable Code
8+
9+
As a good programmer, it is good habit to always write maintainable code. Writing a maintainable code can help later on while updating code, fixing bugs, and enhancing code overtime.
10+
11+
In-order to write maintainable code, we need to follow the following:
12+
13+
- **Increase readability of code**: For a source code to be maintainable, it is important that the code is easily readable, as if the program readability is bad, programmer has to spend more time to understand the code which will slow down the development of program.
14+
- **Write modular code**: As a programmer, it is always a good practice to try and minimize redundancy of code. For an example, if we have to print same thing multiple times through out program execution, we could break that out into a separate function which can be called as needed in multiple places.
15+
- **Documentation & Commenting**: Documenting/ Commenting on code is always a good habit. It is considered to be a good practice to write comments through out the code which explains the code block which will help other programmers who may work on the project in future, and sometimes help you only to understand the code better later on.
16+
- **Consistent naming convention**: It is important to use a proper naming convention through out the program to write a better code. We may use snake_case or camelCase through out the program or other standard naming convention according to style guidelines.
17+
- **Error handling**: As a good programmer, it is necessary to have a code that will handle each and every possible outcome of the code, and it is called error handling. A good programmer should always have a path for the program to follow when it encounters a error so that the error on program doesn't affect other parts of the system like over-use of memory, system crash, etc.
18+
- **Testing**: Along with error handling, testing is also a very important part of coding. As a programmer, it is really important to test the program thoroughly as that desired behavior is achieved.
19+
20+
## Basic Rule
21+
22+
1. Source code is a set of statements, one per line.
23+
2. No end-of line statement character like ';'. Many other languages like C, C++, Java has a end-of line character to denote a end of line.
24+
3. Program execution follows the top-left to right-bottom approach; meaning that the program will execute line by line and one line at a time.
25+
4. There's no special function of the entry-point of the program like int main() in C/ C++.
26+
5. There's a built in `print` function to output to console.
27+
6. There's a built in `input` function to take input from the user.
28+
29+
## Code Readability
30+
31+
- To increase readability we may use whitespace between characters.
32+
- To separate different statements, we use a new line.
33+
- Write meaningful comments, but avoid extra commenting.
34+
- Indentation matters, so do indent code properly.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
sidebar_position: 3
3+
---
4+
5+
# Variable And Literal
6+
7+
## Literals:
8+
9+
### Definition
10+
11+
Fixed values directly written in the code.
12+
13+
### Examples
14+
15+
1. **Numbers**: 4, 102, -21, 4_000_000 (underscores for readability).
16+
2. **Booleans**: True or False.
17+
3. **Text**: "python" or 'This language is fun!'.
18+
4. **Lists**: [1, 2, 3].
19+
20+
## Variables:
21+
22+
### Definition
23+
24+
Names that store values in memory.
25+
26+
### Creation
27+
28+
Use a name, =, and a value (e.g., price = 45.50).
29+
30+
### Naming Rules
31+
32+
Use letters, numbers, or underscores without spaces. Must start with a letter or underscore.
33+
34+
## Data Types:
35+
36+
1. **Integer**: Whole numbers (e.g., -2, 0, 23).
37+
2. **Float**: Numbers with decimals (e.g., 3.14, -0.001).
38+
3. **Boolean**: True or False.
39+
4. **String**: Text (e.g., "Thailand").
40+
5. **NoneType**: Represents no value (None).
41+
42+
## Type Function:
43+
44+
### Usage
45+
46+
Check the type of a value (e.g., type(4) returns int).
47+
48+
## Type Casting:
49+
50+
### Definition
51+
52+
Changing a value from one type to another.
53+
54+
### Examples
55+
56+
1. **String to Float**: float("4.5").
57+
2. **Float to Integer**: int(3.14) (removes decimal).
58+
3. **String to Boolean**: bool("") is False, any other string is True.
59+
60+
## Code Example
61+
62+
```python title="example.py"
63+
# Numeric literals
64+
price = 45.50
65+
age = 24
66+
67+
# Boolean literal
68+
is_printed = True
69+
70+
# String literal
71+
course_name = "Python for Beginners"
72+
73+
# Performing an operation with a variable
74+
tax = price * 0.09
75+
76+
# Receiving input from the console
77+
user_input = input("Enter a number: ")
78+
79+
# Type casting the input to a float
80+
user_number = float(user_input)
81+
82+
# Printing the results
83+
print("Price:", price)
84+
print("Age:", age)
85+
print("Is Printed:", is_printed)
86+
print("Course Name:", course_name)
87+
print("Tax:", tax)
88+
print("User Number:", user_number)
89+
```

0 commit comments

Comments
 (0)