Skip to content

Commit a8c3fc6

Browse files
committed
Add control statement, function and class concepts
1 parent 13558a9 commit a8c3fc6

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

Day-1/control-statements.md

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Control Statements in Python
2+
3+
Control statements in Python are used to control the flow of execution of the program. Here are a few examples:
4+
5+
## If Statement
6+
7+
The `if` statement is used to test a condition. If the condition is true, the block of code inside the `if` statement is executed.
8+
9+
```python
10+
x = 10
11+
if x > 5:
12+
print("x is greater than 5")
13+
```
14+
15+
## If-Else Statement
16+
17+
The `if-else` statement is used to test a condition and execute one block of code if the condition is true and another block of code if the condition is false.
18+
19+
```python
20+
x = 10
21+
if x > 5:
22+
print("x is greater than 5")
23+
else:
24+
print("x is not greater than 5")
25+
```
26+
27+
## Elif Statement
28+
29+
The `elif` statement is used to test multiple conditions. If the first condition is false, it checks the next condition, and so on.
30+
31+
```python
32+
x = 10
33+
if x > 15:
34+
print("x is greater than 15")
35+
elif x > 5:
36+
print("x is greater than 5 but less than or equal to 15")
37+
else:
38+
print("x is 5 or less")
39+
```
40+
41+
### Problems On Conditionals
42+
- [Python If-Else HackerRank](https://www.hackerrank.com/challenges/py-if-else/problem?isFullScreen=true)
43+
- [Check The Status GFG](https://www.geeksforgeeks.org/problems/check-the-status/1?page=1&category=python&difficulty=Easy&sortBy=submissions)
44+
45+
## While Loop
46+
47+
The `while` loop is used to repeat a block of code as long as a condition is true.
48+
49+
```python
50+
x = 0
51+
while x < 5:
52+
print(x)
53+
x += 1
54+
```
55+
56+
## For Loop
57+
58+
The `for` loop is used to iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each item in the sequence.
59+
60+
```python
61+
fruits = ["apple", "banana", "cherry"]
62+
for fruit in fruits:
63+
print(fruit)
64+
```
65+
66+
## Break Statement
67+
68+
The `break` statement is used to exit a loop prematurely.
69+
70+
```python
71+
for i in range(10):
72+
if i == 5:
73+
break
74+
print(i)
75+
```
76+
77+
## Continue Statement
78+
79+
The `continue` statement is used to skip the rest of the code inside a loop for the current iteration and continue with the next iteration.
80+
81+
```python
82+
for i in range(10):
83+
if i % 2 == 0:
84+
continue
85+
print(i)
86+
```
87+
88+
## Pass Statement
89+
90+
The `pass` statement is a null operation; it is used when a statement is required syntactically but no code needs to be executed.
91+
92+
```python
93+
for i in range(10):
94+
if i % 2 == 0:
95+
pass
96+
else:
97+
print(i)
98+
```
99+
100+
### Problems On Conditionals
101+
- [Loops HackerRank](https://www.hackerrank.com/challenges/python-loops/problem?isFullScreen=true)
102+
- [For Loop GFG](https://www.geeksforgeeks.org/problems/for-loop-python/1?page=1&category=python&sortBy=submissions)
103+
- [While Loop GFG](https://www.geeksforgeeks.org/problems/while-loop-in-python/1?page=1&category=python&sortBy=submissions)
104+
105+
106+
These are some of the basic control statements in Python that help in controlling the flow of the program.

Day-1/functions_and_class.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Notes on Functions and Classes in Python
2+
3+
## Functions
4+
5+
### Definition
6+
A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusability.
7+
8+
### Syntax
9+
```python
10+
def function_name(parameters):
11+
"""docstring"""
12+
statement(s)
13+
```
14+
15+
### Example
16+
```python
17+
def greet(name):
18+
"""This function greets the person passed in as a parameter"""
19+
print(f"Hello, {name}!")
20+
21+
greet('Alice')
22+
```
23+
24+
### Key Points
25+
- Functions help break our program into smaller and modular chunks.
26+
- Function definitions begin with the `def` keyword.
27+
- The parameters (arguments) are optional.
28+
- The `return` statement is used to exit a function and go back to the place from where it was called.
29+
30+
## Classes
31+
32+
### Definition
33+
A class is a blueprint for creating objects. Classes encapsulate data for the object and methods to manipulate that data.
34+
35+
### Syntax
36+
```python
37+
class ClassName:
38+
"""docstring"""
39+
class_variable = 'value'
40+
41+
def __init__(self, parameters):
42+
self.instance_variable = parameters
43+
44+
def method(self):
45+
statement(s)
46+
```
47+
48+
### Example
49+
```python
50+
class Dog:
51+
"""A simple class to model a dog"""
52+
53+
def __init__(self, name, age):
54+
self.name = name
55+
self.age = age
56+
57+
def bark(self):
58+
print(f"{self.name} says woof!")
59+
60+
my_dog = Dog('Buddy', 3)
61+
my_dog.bark()
62+
```
63+
64+
### Key Points
65+
- Classes provide a means of bundling data and functionality together.
66+
- Creating a new class creates a new type of object, allowing new instances of that type to be made.
67+
- The `__init__` method initializes the object's attributes.
68+
- Methods are functions that belong to the class.

0 commit comments

Comments
 (0)