Lesson – 106 : Python માં Iterators, Modules, Dates અને Math
Python એક ખૂબ જ લોકપ્રિય અને સરળ પ્રોગ્રામિંગ ભાષા છે. તેમાં વિવિધ ફીચર્સ છે જે પ્રોગ્રામિંગને સરળ, જલદી અને વધારે શક્તિશાળી બનાવે છે.
🔶 1. Iterators in Python (ઇટરેટર્સ)
Iterator એ એક એવો ઓબ્જેક્ટ છે જેનાથી આપણે values એક પછી એક મેળવી શકીએ.
✔ Iterator શું છે?
Iterator એ એવો ઓબ્જેક્ટ છે જેમાં __iter__() અને __next__() method હોય છે.
✔ Iterator ક્યાં કામ આવે?
-
લૂપ્સમાં
-
ડેટા એક પછી એકอ่าน કરવા માટે
-
Memory-efficient પ્રોગ્રામિંગ માટે
✔ ઉદાહરણ:
my_list = [10, 20, 30]
itr = iter(my_list)
print(next(itr)) # 10
print(next(itr)) # 20
print(next(itr)) # 30
🔶 2. Modules in Python (મોડ્યુલ્સ)
Module એટલે Python કોડને અલગ file માં રાખવાનો એક system. Modules નો ઉપયોગ reuse અને code management માટે થાય છે.
✔ Module શું છે?
Python file (.py) જેમાં functions, classes, variables હોય તેને module કહેવામાં આવે છે.
✔ Types of Modules:
-
Built-in modules (Python માં અગાઉથી આપેલા)
-
User-defined modules (જે આપણે બનાવીએ)
✔ Built-in module example:
import math
print(math.sqrt(25)) # 5.0
✔ User-defined module example:
module file: mymodule.py
def greet(name):
return f"Hello, {name}"
main program:
import mymodule
print(mymodule.greet("Saiyam"))
🔶 3. Dates in Python (તારીખ અને સમય)
Python માં તારીખ અને સમય સાથે કામ કરવા માટે datetime module વપરાય છે.
✔ તારીખ મેળવવી:
import datetime
today = datetime.datetime.now()
print(today)
✔ માત્ર તારીખ:
print(today.date())
✔ માત્ર સમય:
print(today.time())
✔ ચોક્કસ તારીખ બનાવવી:
d = datetime.datetime(2025, 5, 10)
print(d)
✔ તારીખનું ફોર્મેટિંગ:
print(today.strftime("%d-%m-%Y"))
print(today.strftime("%H:%M:%S"))
🔶 4. Math in Python (ગણિતીય ફંક્શન્સ)
Python માં math module નો ઉપયોગ કરીને ઘણી ગણિતીય operations સરળ બને છે.
✔ ઉપયોગ:
import math
✔ મહત્વપૂર્ણ functions:
| Function | ઉપયોગ |
|---|---|
math.sqrt(x) |
√x — સ્ક્વેર રૂટ |
math.pow(x, y) |
x^y — power |
math.ceil(x) |
Value ઉપર round કરે |
math.floor(x) |
Value નીચે round કરે |
math.pi |
3.14… પાઇ value |
math.sin(x) |
સિન ફંક્શન |
math.log(x) |
લૉગારિધમ |
✔ ઉદાહરણ:
import math
print(math.sqrt(49)) # 7
print(math.ceil(4.3)) # 5
print(math.floor(4.9)) # 4
print(math.pi) # 3.141592653589793
આ ચારેય વિષયો Python માં programmingને વધુ શક્તિશાળી, professional અને સરળ બનાવે છે.