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

משתנים וסוגים


Python היא שפה מונחית עצמים לחלוטין, ואינה "מוקלדת סטטית". אין צורך להצהיר על משתנים לפני השימוש בהם, או להצהיר על סוגם. כל משתנה ב-Python הוא עצם.

מדריך זה יסקור כמה סוגים בסיסיים של משתנים.

מספרים

Python תומך בשני סוגי מספרים - שלמים (מספרים שלמים) ומספרים עשרוניים (נקודת צף). (היא גם תומכת במספרים מרוכבים, שלא יוסברו במדריך זה).

כדי להגדיר מספר שלם, השתמש בתחביר הבא:

myint = 7
print(myint)

כדי להגדיר מספר עשרוני, תוכל להשתמש באחת מההערות הבאות:

myfloat = 7.0
print(myfloat)
myfloat = float(7)
print(myfloat)

מחרוזות

מחרוזות מוגדרות או עם גרש יחיד או עם גרש כפול.

mystring = 'hello'
print(mystring)
mystring = "hello"
print(mystring)

ההבדל בין השניים הוא ששימוש בגרש כפול מקל על הכללת גרשיים (ואילו אלה יסיימו את המחרוזת אם משתמשים בגרש יחיד).

mystring = "Don't worry about apostrophes"
print(mystring)

ישנן וריאציות נוספות להגדרת מחרוזות שמקלות על הכללת אלמנטים כמו חזרות קרון, סלאשים לאחור ותווים של Unicode. אלה הם מעבר לתחום של המדריך הזה, אך מכוסים ב-Python documentation.

ניתן לבצע אופרטורים פשוטים על מספרים ומחרוזות:

one = 1
two = 2
three = one + two
print(three)

hello = "hello"
world = "world"
helloworld = hello + " " + world
print(helloworld)

ניתן לבצע השמות ביותר ממשתנה אחד "בו זמנית" באותה שורה כך

a, b = 3, 4
print(a, b)

שילוב של אופרטורים בין מספרים ומחרוזות אינו נתמך:

# זה לא יעבוד!
one = 1
two = 2
hello = "hello"

print(one + two + hello)

תרגיל

מטרת התרגיל הזה היא ליצור מחרוזת, מספר שלם ומספר עשרוני. המחרוזת צריכה להיקרא mystring וצריכה להכיל את המילה "hello". מספר הנקודה הצפה צריך להיקרא myfloat וצריך להכיל את המספר 10.0, והמספר השלם צריך להיקרא myint וצריך להכיל את המספר 20.

# change this code mystring = None myfloat = None myint = None # testing code if mystring == "hello": print("String: %s" % mystring) if isinstance(myfloat, float) and myfloat == 10.0: print("Float: %f" % myfloat) if isinstance(myint, int) and myint == 20: print("Integer: %d" % myint) # change this code mystring = "hello" myfloat = 10.0 myint = 20 # testing code if mystring == "hello": print("String: %s" % mystring) if isinstance(myfloat, float) and myfloat == 10.0: print("Float: %f" % myfloat) if isinstance(myint, int) and myint == 20: print("Integer: %d" % myint) test_object('mystring', incorrect_msg="Don't forget to change `mystring` to the correct value from the exercise description.") test_object('myfloat', incorrect_msg="Don't forget to change `myfloat` to the correct value from the exercise description.") test_object('myint', incorrect_msg="Don't forget to change `myint` to the correct value from the exercise description.") test_output_contains("String: hello",no_output_msg= "Make sure your string matches exactly to the exercise desciption.") test_output_contains("Float: 10.000000",no_output_msg= "Make sure your float matches exactly to the exercise desciption.") test_output_contains("Integer: 20",no_output_msg= "Make sure your integer matches exactly to the exercise desciption.") success_msg("Great job!")

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!

Previous Tutorial Next Tutorial Take the Test
Copyright © learnpython.org. Read our Terms of Use and Privacy Policy