متد ها و توابع کاربردی انواع داده در پایتون

متد ها و توابع کاربردی انواع داده در پایتون

method function
آموزش مقدماتی پایتون

متد ها و توابع کاربردی انواع داده در پایتون

متد ها و توابع کاربردی انواع داده در پایتون

متدهای پرکاربرد:

number = 18  # در مبنای دو: 10010
print(number.bit_length())  # خروجی: 5
number = 1000
byte_data = number.to_bytes(2, byteorder='big')
print(byte_data)  # خروجی: b'\x03\xe8'
score_loss = -25
print(abs(score_loss))  # خروجی: 25
num = 15
print(bin(num))  # خروجی: '0b1111'
print(hex(num))  # خروجی: '0xf'
grade1, grade2, grade3 = 14, 19, 11
print(max(grade1, grade2, grade3))  # خروجی: 19
print(min(grade1, grade2, grade3))  # خروجی: 11

متدهای پرکاربرد:

temp1 = 37.0
temp2 = 37.5
print(temp1.is_integer())  # خروجی: True
print(temp2.is_integer())  # خروجی: False
ratio = 0.75
print(ratio.as_integer_ratio())  # خروجی: (3, 4)
average = 17.6583
print(round(average, 2))  # خروجی: 17.66
print(round(average))     # خروجی: 18
temperature_change = -3.4
print(abs(temperature_change))  # خروجی: 3.4
price1, price2 = 19.99, 14.50
print(max(price1, price2))  # خروجی: 19.99
print(min(price1, price2))  # خروجی: 14.50

ویژگی‌ها و متدهای پرکاربرد:

z = 4 + 7j
print(z.real)  # خروجی: 4.0
print(z.imag)  # خروجی: 7.0
z = 4 + 7j
print(z.conjugate())  # خروجی: (4-7j)
z = 3 + 4j
print(abs(z))  # خروجی: 5.0

توابع توکار اعمال‌پذیر:

print(bool(""))       # خروجی: False (متن خالی)
print(bool("Ali"))    # خروجی: True
print(bool(0))        # خروجی: False
print(bool(15))       # خروجی: True
print(int(True))   # خروجی: 1
print(int(False))  # خروجی: 0
print(max(True, False))  # خروجی: True
print(min(True, False))  # خروجی: False

رشته‌ها برای ذخیره متن استفاده می‌شوند و تنوع بالایی از متدها دارند.

متدهای پرکاربرد:

  • upper() و lower(): تغییر تمامی حروف به بزرگ یا کوچک.
city = "Tehran"
print(city.upper())  # خروجی: 'TEHRAN'
print(city.lower())  # خروجی: 'tehran'
username = "  user_student  "
print(username.strip())  # خروجی: 'user_student'
fruits = "apple,banana,orange"
print(fruits.split(","))  # خروجی: ['apple', 'banana', 'orange']
words = ["Learn", "Python", "Today"]
print(" ".join(words))  # خروجی: 'Learn Python Today'
message = "Hello World"
print(message.replace("World", "Python"))  # خروجی: 'Hello Python'
file_name = "report.pdf"
print(file_name.endswith(".pdf"))  # خروجی: True
print(file_name.startswith("data")) # خروجی: False
phone = "09120000000"
print(phone.isdigit())  # خروجی: True
name = "Sara"
print(len(name))  # خروجی: 4
letters = "python"
print(max(letters))  # خروجی: 'y'
print(min(letters))  # خروجی: 'h'
text_val = "100.75"
num_val = -42.85

print(f"1. type(): {type(text_val)}")
print(f"2. len(): {len(text_val)}")
print(f"3. float(): {float(text_val)}")
print(f"4. int(): {int(100.75)}")
print(f"5. str(): {str(500) + ' تومان'}")
print(f"6. abs(): {abs(num_val)}")
print(f"7. round(): {round(num_val, 1)}")
print(f"8. range(): {list(range(1, 6))}")
print("9. print() با پارامتر sep و end:", "پایتون", "زبان", "قدرتمند", sep=" - ", end="!\n")
scores = [15, 18, 12, 14]
is_all_passed = all(score >= 10 for score in scores)
print(is_all_passed)  # خروجی: True
scores = [15, 8, 19, 14]
has_failed = any(score < 10 for score in scores)
print(has_failed)  # خروجی: True

فکر خود را اینجا بگذارید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *