Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!
Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount
אופרטורים בסיסיים
אופרטורים אריתמטיים
כמו בכל שפת תכנות אחרת, ניתן להשתמש באופרטורים של חיבור, חיסור, כפל וחילוק עם מספרים.
number = 1 + 2 * 3 / 4.0
print(number)
נסה לחזות מה תהיה התשובה. האם הפייתון עוקב אחר סדר הפעולות?
אופרטור נוסף זמין הוא אופרטור המודולו (%) המחזיר את שארית החילוק. dividend % divisor = remainder.
remainder = 11 % 3
print(remainder)
שימוש בשני סמלי כפל יוצרים יחס חזקתי.
squared = 7 ** 2
cubed = 2 ** 3
print(squared)
print(cubed)
שימוש באופרטורים עם מיתרים
פייתון תומכת בחיבור מיתרים באמצעות אופרטור החיבור:
helloworld = "hello" + " " + "world"
print(helloworld)
פייתון גם תומכת בכפל מיתרים ליצירת סדרה שחוזרת על עצמה:
lotsofhellos = "hello" * 10
print(lotsofhellos)
שימוש באופרטורים עם רשימות
ניתן לחבר רשימות באמצעות אופרטור החיבור:
even_numbers = [2,4,6,8]
odd_numbers = [1,3,5,7]
all_numbers = odd_numbers + even_numbers
print(all_numbers)
כמו במיתרים, פייתון תומכת ביצירת רשימות חדשות עם סדרות חוזרות באמצעות אופרטור הכפל:
print([1,2,3] * 3)
תרגיל
מטרת התרגיל הזה היא ליצור שתי רשימות שנקראות x_list
ו-y_list
, אשר מכילות 10 פעמים את המשתנים x
ו-y
, בהתאמה. כמו כן, נדרש מכם ליצור רשימה שנקראת big_list
, אשר מכילה את המשתנים x
ו-y
כל אחד 10 פעמים, על ידי חיבור שתי הרשימות שיצרתם.
x = object()
y = object()
# TODO: change this code
x_list = [x]
y_list = [y]
big_list = []
print("x_list contains %d objects" % len(x_list))
print("y_list contains %d objects" % len(y_list))
print("big_list contains %d objects" % len(big_list))
# testing code
if x_list.count(x) == 10 and y_list.count(y) == 10:
print("Almost there...")
if big_list.count(x) == 10 and big_list.count(y) == 10:
print("Great!")
x = object()
y = object()
# TODO: change this code
x_list = [x] * 10
y_list = [y] * 10
big_list = x_list + y_list
print("x_list contains %d objects" % len(x_list))
print("y_list contains %d objects" % len(y_list))
print("big_list contains %d objects" % len(big_list))
# testing code
if x_list.count(x) == 10 and y_list.count(y) == 10:
print("Almost there...")
if big_list.count(x) == 10 and big_list.count(y) == 10:
print("Great!")
Ex().check_object('x_list').has_equal_value(expr_code = 'len(x_list)')
Ex().check_object('y_list').has_equal_value(expr_code = 'len(y_list)')
Ex().check_object('big_list').has_equal_value(expr_code = 'len(big_list)')
success_msg('Good work!')
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!