Skip to content

لیکچر 43: معیاری ماڈیولز کا استعمال

Python environment

پائتھن کورس کا لیکچر 43۔ پائتھن کی معیاری لائبریری سے دو مفید ماڈیولز: math اور random کو دریافت کریں۔

معیاری لائبریری کو دریافت کرنا

پائتھن کی معیاری لائبریری میں مختلف قسم کے کاموں کے لیے سینکڑوں ماڈیولز شامل ہیں۔ آئیے دو سب سے زیادہ استعمال ہونے والے ماڈیولز پر گہری نظر ڈالتے ہیں: math اور random۔

math ماڈیول

math ماڈیول بنیادی آپریٹرز سے ہٹ کر ریاضیاتی فنکشنز تک رسائی فراہم کرتا ہے۔

import math

# math.sqrt() - finds the square root
print(math.sqrt(64)) # Output: 8.0

# math.ceil() - rounds a number UP to the nearest integer
print(math.ceil(4.2)) # Output: 5

# math.floor() - rounds a number DOWN to the nearest integer
print(math.floor(4.8)) # Output: 4

# math.pi - the constant Pi
radius = 5
area = math.pi * (radius ** 2)
print(f"The area of a circle with radius {radius} is {area}")

random ماڈیول

random ماڈیول بے ترتیب نمبرز بنانے کے لیے استعمال ہوتا ہے، جو گیمز، سمیولیشنز، اور بے ترتیب اشیاء کو منتخب کرنے کے لیے مفید ہے۔

import random

# random.randint(a, b) - returns a random integer between a and b (inclusive)
random_number = random.randint(1, 100)
print(f"A random number between 1 and 100: {random_number}")

# random.choice(sequence) - returns a random element from a sequence (like a list)
players = ['charles', 'martina', 'michael', 'florence']
first_player = random.choice(players)
print(f"The first player will be: {first_player.title()}")

# random.shuffle(list) - shuffles the items of a list in place (modifies the original list)
cards = ['Ace', 'King', 'Queen', 'Jack']
random.shuffle(cards)
print("The shuffled cards are:", cards)
لیکچر کا خلاصہ

آپ نے پائتھن کی معیاری لائبریری سے دو ضروری ماڈیولز کو دریافت کیا ہے۔ math ماڈیول آپ کو اعلیٰ ریاضیاتی فنکشنز اور مستقل تک رسائی فراہم کرتا ہے، جبکہ random ماڈیول آپ کو بے ترتیب پن کے ساتھ کام کرنے کی اجازت دیتا ہے، جو بہت سے دلچسپ پروگراموں کا ایک اہم جزو ہے۔

اگلی بار

ہم اپنے کوڈ کو ماڈیولز میں منظم کرنا سیکھیں گے تاکہ ہم اپنے فنکشنز کو مختلف پروگراموں میں دوبارہ استعمال کر سکیں اپنے ماڈیولز بنانا میں۔

لیکچر 42: ماڈیولز کا تعارف – پائتھن کورس

Python environment

Leave a Reply

Your email address will not be published. Required fields are marked *