Range concept
- Get link
- X
- Other Apps
Python method example
In Python, a method is a function that belongs to an object and is called using the dot (.) operator.
1. String Method Example
name = "nilesh gupta" print(name.upper()) print(name.lower()) print(name.title())
Output:
NILESH GUPTA nilesh gupta Nilesh Gupta
2. List Method Example
students = ["Amit", "Rahul", "Priya"] students.append("Neha") print(students) students.remove("Rahul") print(students)
Output:
['Amit', 'Rahul', 'Priya', 'Neha'] ['Amit', 'Priya', 'Neha']
3. User-Defined Method Example
class Student: def display(self): print("Welcome to Python") s = Student() s.display()
Output:
Welcome to Python
Here, display() is a method of the Student class, and s.display() calls that method.
Python range function example
Python range() Function
The range() function is used to generate a sequence of numbers, commonly with a for loop.
1. Basic Example
for i in range(5): print(i)
Output:
0 1 2 3 4
2. range(start, stop)
for i in range(1, 6): print(i)
Output:
1 2 3 4 5
The
stopvalue (6) is not included.
3. range(start, stop, step)
for i in range(2, 11, 2): print(i)
Output:
2 4 6 8 10
4. Reverse Range
for i in range(5, 0, -1): print(i)
Output:
5 4 3 2 1
Syntax
range(start, stop, step)
start → starting number
stop → ending limit, not included
step → difference between numbers
- Get link
- X
- Other Apps
Comments
Post a Comment