Printing Odd Numbers Between O and 100 in Python ?
Problem: : You want to print the odd num bers between 0 and 100 in Python using list comprehension.
Solution: You can achieve this by using list comprehension to generate a list of odd numbers and then print the list.
Program
def myfun():
odd_numbers = [x for x in range(101) if x % 2 != 0]
print (odd_numbers)
if __name__=="__main__":
myfun()
Output:
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
This code iterates through numbers from 0 to 100 and includes only those that are not divisible by 2, effectively capturing all the odd numbers. Finally, it prints the list of odd numbers, which will be the odd numbers from l to 99.
More Questions
5 . Printing Odd Numbers Between O and 100
6 . Finding Unique Values in a List
7 . Accessing Attributes of Functions
8 . Performing Bitwise XOR
9 . Swapping Variable Values Without an Intermediary Variable
10 . Introspection and Reflection
11 . Understanding Mixins in Object-Oriented Programming
12 . Exploring the CheeseShop
13 . Virtual Environments in Python
14 . PEP 8 The Python Enhancement Proposal 8
15 . Modifying Strings in Python