The range() function in Python is a powerful tool for generating sequences of numbers. Here's a breakdown of how it works and some examples to illustrate its usage:
Functionality:
range() generates a sequence of integers.
By default, it starts from 0, increments by 1, and stops before a specified end value (which is not included).
Syntax:
Python
range(start, stop, step)
start (optional): This argument defines the starting number of the sequence. If omitted, it defaults to 0.
stop (required): This argument specifies the number before which the sequence stops. It's not included in the sequence itself.
step (optional): This argument defines the increment between each number in the sequence. By default, it's 1. You can use a positive or negative step value to control the direction of the sequence.
Examples:
Generate numbers from 0 to 4 :
Python
numbers = range(5)
print(list(numbers)) # Output: [0, 1, 2, 3,4]
Generate numbers from 1 to 10 (excluding 10) with a step of 2:
Python
numbers = range(1, 11, 2)
print(list(numbers)) # Output: [1, 3, 5, 7, 9]
Generate numbers in descending order from 5 to 1 (excluding 0):
Python
numbers = range(5, 0, -1)
print(list(numbers)) # Output: [5, 4, 3, 2, 1]
Key Points:
range() itself doesn't store the actual numbers. It's an iterable object that generates numbers on demand when used in a loop or converted to a list using list().
The stop value is exclusive, meaning the sequence won't include that number.
I hope this explanation clarifies the range() function in Python! Feel free to ask if you have any more questions.
Types of Conditional Statements
if statements: Execute code blocks based on whether a condition is True.
Python
if age >= 18:
print("You are eligible to vote.")if-else statements: Execute one code block if a condition is True, otherwise execute a different code block.
Python
if number % 2 == 0:
print("Number is even.")
else:
print("Number is odd.")if-elif-else statements: Handle multiple conditions in a sequential manner.
Python
grade = 85
if grade >= 90:
print("Excellent")
elif grade >= 80:
print("Very Good")
elif grade >= 70:
print("Good")
else:
print("Needs improvement")
Types of Loops
for loops: Iterate over a sequence (list, string, tuple, etc.)
Python
colors = ["red", "blue", "green"]
for color in colors:
print(color)while loops: Continue looping until a condition is no longer True.
Python
count = 0
while count < 5:
print("Count:", count)
count += 1
Examples of Conditional Statements and Loops Combined
Finding even numbers in a list:
Python
numbers = [1, 5, 8, 22, 13, 4]
for num in numbers:
if num % 2 == 0:
print(num, "is even")Calculating factorial with user input:
Python
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print("Factorial doesn't exist for negative numbers")
elif num == 0:
print("Factorial of 0 is 1")
else:
for i in range(1, num + 1):
factorial *= i
print("Factorial of", num, "is", factorial)Guessing game:
Python
import random
secret_number = random.randint(1, 20)
for guesses in range(1, 7):
guess = int(input("Guess a number between 1 and 20: "))
if guess < secret_number:
print("Too low!")
elif guess > secret_number:
print("Too high!")
else:
print("You guessed it in", guesses, "tries!")
break
Tips
Pay attention to indentation. Incorrect indentation breaks code functionality.
Use meaningful variable names to enhance code readability.
Let me know if you'd like to explore more specific scenarios or problem types!
Sources