هذه صفحة لقطة ثنائية اللغة تم حفظها بواسطة المستخدم في 2025-7-30 22:05 لـ https://app.immersivetranslate.com/pdf-pro/70dbe241-7b1d-4c84-9337-9e6067d9a235/، مقدمة بدعم ثنائي اللغة من قبل الترجمة الغامرة. تعلم كيفية الحفظ؟

2

VARIABLES AND SIMPLE DATA TYPES
المتغيرات وأنواع البيانات البسيطة

In this chapter you’ll learn about the different kinds of data you can work with in your Python programs. You’ll also learn how to use variables to represent data in your programs.
في هذا الفصل، ستتعلم أنواع البيانات المختلفة التي يمكنك العمل عليها في برامج بايثون. ستتعلم أيضًا كيفية استخدام المتغيرات لتمثيل البيانات في برامجك.

What Really Happens When You Run hello_world.py
ماذا يحدث حقًا عند تشغيل hello_world.py

Let’s take a closer look at what Python does when you run hello_world.py. As it turns out, Python does a fair amount of work, even when it runs a simple program:
لنلقِ نظرةً عن كثب على ما يفعله بايثون عند تشغيل hello_world.py. وكما اتضح، يُنجز بايثون قدرًا لا بأس به من العمل، حتى عند تشغيل برنامج بسيط:

hello_world.py
print(“Hello Python world!”)
print("مرحبا بعالم بايثون!")

When you run this code, you should see this output:
عندما تقوم بتشغيل هذا الكود، يجب أن ترى هذا الإخراج:

Hello Python world!  مرحباً بعالم بايثون!
When you run the file hello_world.py, the ending .py indicates that the file is a Python program. Your editor then runs the file through the Python interpreter, which reads through the program and determines what each word in the program means. For example, when the interpreter sees the word print followed by parentheses, it prints to the screen whatever is inside the parentheses.
عند تشغيل الملف hello_world.py، تشير النهاية .py إلى أن الملف برنامج بايثون. ثم يُشغّل مُحرّر النصوص الملف عبر مُفسّر بايثون، الذي يقرأ البرنامج ويحدد معنى كل كلمة فيه. على سبيل المثال، عندما يرى المُفسّر كلمة "print" متبوعة بأقواس، فإنه يطبع على الشاشة ما بين الأقواس.
As you write your programs, your editor highlights different parts of your program in different ways. For example, it recognizes that print() is the name of a function and displays that word in one color. It recognizes that “Hello Python world!” is not Python code and displays that phrase in a different color. This feature is called syntax highlighting and is quite useful as you start to write your own programs.
أثناء كتابة برامجك، يُبرز مُحرّر النصوص أجزاءً مُختلفة من برنامجك بطرق مُختلفة. على سبيل المثال، يُميّز المُحرّر أن دالة print() هي اسم دالة، فيعرض الكلمة بلون مُحدّد. كما يُميّز أن جملة "Hello Python world!" ليست شيفرةً لبايثون، فيعرضها بلون مُختلف. تُسمّى هذه الميزة "تمييز بناء الجملة"، وهي مُفيدة جدًّا عند البدء بكتابة برامجك.

Variables  المتغيرات

Let’s try using a variable in hello_world.py. Add a new line at the beginning of the file, and modify the second line:
لنجرب استخدام متغير في ملف hello_world.py. أضف سطرًا جديدًا في بداية الملف، وعدّل السطر الثاني:

hello_world.py
message = “Hello Python world!”
الرسالة = "مرحبا بعالم بايثون!"

print(message)  طباعة (رسالة)
Run this program to see what happens. You should see the same output you saw previously:
شغّل هذا البرنامج لترى ما سيحدث. من المفترض أن ترى نفس النتيجة التي رأيتها سابقًا:
Hello Python world!  مرحباً بعالم بايثون!
We’ve added a variable named message. Every variable is connected to a value, which is the information associated with that variable. In this case the value is the “Hello Python world!” text.
أضفنا متغيرًا باسم "رسالة". كل متغير مرتبط بقيمة، وهي المعلومات المرتبطة به. في هذه الحالة، القيمة هي نص "مرحبًا بعالم بايثون!".
Adding a variable makes a little more work for the Python interpreter. When it processes the first line, it associates the variable message with the “Hello Python world!” text. When it reaches the second line, it prints the value associated with message to the screen.
إضافة متغير يُسهّل على مُفسّر بايثون بعض العمل. عند معالجة السطر الأول، يربط رسالة المتغير بنص "مرحبًا يا عالم بايثون!". وعند الوصول إلى السطر الثاني، يطبع القيمة المرتبطة بالرسالة على الشاشة.
Let’s expand on this program by modifying hello_world.py to print a second message. Add a blank line to hello_world.py, and then add two new lines of code:
لنُوسّع هذا البرنامج بتعديل ملف hello_world.py لطباعة رسالة ثانية. أضف سطرًا فارغًا إلى hello_world.py، ثم أضف سطرين جديدين من التعليمات البرمجية:
message = "Hello Python world!"
print(message)
message = "Hello Python Crash Course world!"
print(message)
Now when you run hello_world.py, you should see two lines of output:
الآن عندما تقوم بتشغيل hello_world.py، يجب أن ترى سطرين من الإخراج:
Hello Python world!
Hello Python Crash Course world!
You can change the value of a variable in your program at any time, and Python will always keep track of its current value.
يمكنك تغيير قيمة متغير في برنامجك في أي وقت، وسيقوم Python دائمًا بتتبع قيمته الحالية.

Naming and Using Variables
تسمية المتغيرات واستخدامها

When you’re using variables in Python, you need to adhere to a few rules and guidelines. Breaking some of these rules will cause errors; other guidelines just help you write code that’s easier to read and understand. Be sure to keep the following variable rules in mind:
عند استخدام المتغيرات في بايثون، عليك الالتزام ببعض القواعد والإرشادات. مخالفة بعض هذه القواعد قد تُسبب أخطاءً؛ بينما تُساعدك إرشادات أخرى على كتابة شيفرة أسهل قراءةً وفهمًا. تأكد من مراعاة قواعد المتغيرات التالية:
  • Variable names can contain only letters, numbers, and underscores. They can start with a letter or an underscore, but not with a number. For instance, you can call a variable message_ 1 but not 1 _ 1 _ 1_1 \_message.
    يمكن أن تحتوي أسماء المتغيرات على أحرف وأرقام وشرطات سفلية فقط. يمكن أن تبدأ بحرف أو شرطة سفلية، ولكن ليس برقم. على سبيل المثال، يمكنك تسمية متغير message_1 ولكن ليس 1 _ 1 _ 1_1 \_ message.
  • Spaces are not allowed in variable names, but underscores can be used to separate words in variable names. For example, greeting_message works, but greeting message will cause errors.
    لا يُسمح باستخدام المسافات في أسماء المتغيرات، ولكن يُمكن استخدام الشرطة السفلية لفصل الكلمات في أسماء المتغيرات. على سبيل المثال، تعمل رسالة التحية، ولكن رسالة التحية ستُسبب أخطاءً.
  • Avoid using Python keywords and function names as variable names; that is, do not use words that Python has reserved for a particular programmatic purpose, such as the word print. (See “Python Keywords and Built-in Functions” on page 471.)
    تجنب استخدام كلمات بايثون المفتاحية وأسماء الدوال كأسماء للمتغيرات؛ أي لا تستخدم كلماتٍ خصصتها بايثون لغرض برمجي معين، مثل كلمة "طباعة". (انظر "كلمات بايثون المفتاحية والدوال المدمجة" في الصفحة ٤٧١.)
  • Variable names should be short but descriptive. For example, name is better than n n nn, student_name is better than s n s n s_(-)ns_{-} n, and name_length is better than length_of_persons_name.
    يجب أن تكون أسماء المتغيرات قصيرة وواضحة. على سبيل المثال، الاسم أفضل من n n nn ، واسم الطالب أفضل من s n s n s_(-)ns_{-} n ، وطول الاسم أفضل من طول اسم الشخص.
  • Be careful when using the lowercase letter l l ll and the uppercase letter O O OO because they could be confused with the numbers 1 and 0 .
    كن حذرًا عند استخدام الحرف الصغير l l ll والحرف الكبير O O OO لأنه يمكن الخلط بينهما وبين الرقمين 1 و0.
It can take some practice to learn how to create good variable names, especially as your programs become more interesting and complicated. As you write more programs and start to read through other people’s code, you’ll get better at coming up with meaningful names.
قد يتطلب الأمر بعض الممارسة لتعلم كيفية إنشاء أسماء جيدة للمتغيرات، خاصةً مع ازدياد تعقيد برامجك وتعقيدها. مع كتابة المزيد من البرامج وبدء قراءة أكواد الآخرين، ستتحسن قدرتك على ابتكار أسماء ذات معنى.
The Python variables you’re using at this point should be lowercase. You won’t get errors if you use uppercase letters, but uppercase letters in variable names have special meanings that we’ll discuss in later chapters.
يجب أن تكون متغيرات بايثون التي تستخدمها الآن بأحرف صغيرة. لن تواجه أي أخطاء إذا استخدمت أحرفًا كبيرة، ولكن للأحرف الكبيرة في أسماء المتغيرات معانٍ خاصة سنناقشها في الفصول اللاحقة.

Avoiding Name Errors When Using Variables
تجنب أخطاء الأسماء عند استخدام المتغيرات

Every programmer makes mistakes, and most make mistakes every day. Although good programmers might create errors, they also know how to respond to those errors efficiently. Let’s look at an error you’re likely to make early on and learn how to fix it.
كل مبرمج يرتكب أخطاءً، ومعظمهم يرتكبها يوميًا. مع أن المبرمجين الجيدين قد يرتكبون أخطاءً، إلا أنهم يعرفون أيضًا كيفية التعامل معها بكفاءة. لنلقِ نظرة على خطأ من المحتمل أن ترتكبه في البداية ونتعلم كيفية إصلاحه.
We’ll write some code that generates an error on purpose. Enter the following code, including the misspelled word mesage shown in bold:
سنكتب شيفرةً تُولّد خطأً عمدًا. أدخل الشيفرة التالية، متضمنةً كلمة "mesage" المكتوبة بخط غامق:

  1. message = “Hello Python Crash Course reader!”
    الرسالة = "مرحباً بقارئ دورة Python Crash Course!"

    print(mesage)  طباعة (رسالة)