ตัวแปรและชนิดข้อมูลใน Python

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

ตัวแปรในภาษา Python มีหน้าที่สำคัญอย่างไรในการเขียนโปรแกรม?

  • ใช้ในการกำหนดชนิดของข้อมูลที่จะใช้ในโปรแกรมเท่านั้น
  • ใช้ในการกำหนดขนาดของโปรแกรม
  • ใช้ในการเก็บข้อมูลที่สามารถเปลี่ยนแปลงได้ในระหว่างการทำงานของโปรแกรม (correct)
  • ใช้ในการแสดงผลลัพธ์ของโปรแกรมเท่านั้น

ถ้าต้องการแสดงผลลัพธ์ “The score is 85 points.” โดยใช้ตัวแปร score และข้อความ ควรเขียนโค้ดอย่างไร?

  • `score = 85; print("The score is " + score + " points.")`
  • `print("The score is {score} points.")`
  • `score = 85; print(f"The score is {score} points.")` (correct)
  • `print("The score is score points.")`

ชนิดข้อมูลใดใน Python ที่เหมาะสมที่สุดสำหรับการเก็บค่าความจริงหรือเท็จ?

  • Integer
  • Float
  • Boolean (correct)
  • String

ข้อใดคือความแตกต่างที่สำคัญระหว่างชนิดข้อมูล Integer และ Float ใน Python?

<p>Integer ใช้เก็บตัวเลขจำนวนเต็ม ส่วน Float ใช้เก็บตัวเลขที่มีทศนิยม (A)</p> Signup and view all the answers

ถ้าต้องการเปลี่ยนค่าของตัวแปร x จาก 5 เป็น 10 ใน Python ต้องทำอย่างไร?

<p><code>x = 10</code> (B)</p> Signup and view all the answers

ถ้ากำหนดให้ name = "Python" และ age = 30 ข้อใดคือผลลัพธ์ของการ print(f"{name} is {age} years old.")

<p>Python is 30 years old. (A)</p> Signup and view all the answers

ถ้าต้องการกำหนดให้ตัวแปร is_valid มีค่าเป็นจริง (True) จะต้องเขียนอย่างไร?

<p><code>is_valid = True</code> (D)</p> Signup and view all the answers

สมมติว่าคุณมีตัวแปร radius = 5 และต้องการคำนวณพื้นที่วงกลม (area) โดยใช้สูตร $\pi r^2$ คุณจะเขียนโค้ด Python อย่างไร?

<p>ข้อ A และ C ถูก (A)</p> Signup and view all the answers

ถ้าคุณพยายามใช้ตัวแปรที่ยังไม่ได้กำหนดค่าใน Python จะเกิดอะไรขึ้น?

<p>โปรแกรมจะแสดงข้อผิดพลาด (Error) (B)</p> Signup and view all the answers

พิจารณาโค้ดต่อไปนี้: x = 10; y = "10"; x และ y มีความแตกต่างกันอย่างไร?

<p><code>x</code> เป็น Integer, <code>y</code> เป็น String (B)</p> Signup and view all the answers

ข้อใดคือเหตุผลสำคัญที่สุดในการใช้ตัวแปรในโปรแกรม?

<p>ทำให้ง่ายต่อการแก้ไขและจัดการข้อมูล (A)</p> Signup and view all the answers

ถ้าคุณมีตัวแปร price = 99.99 คุณควรใช้ชนิดข้อมูลใด?

<p>Float (D)</p> Signup and view all the answers

พิจารณาโค้ด: name = "Alice"; print("Hello, " + name) จะเกิดอะไรขึ้นถ้าคุณรันโค้ดนี้?

<p>Hello, Alice (D)</p> Signup and view all the answers

ถ้าต้องการตรวจสอบว่าตัวแปร age มีค่ามากกว่า 18 หรือไม่ ควรเขียนอย่างไร?

<p><code>age &gt; 18</code> (B)</p> Signup and view all the answers

ถ้าคุณมีตัวแปร message = "Hello, World!" และต้องการเปลี่ยนเป็นตัวพิมพ์เล็กทั้งหมด จะต้องใช้คำสั่งใด?

<p><code>message.lower()</code> (B)</p> Signup and view all the answers

สมมติว่าคุณมีตัวแปร count = 0 และต้องการเพิ่มค่าขึ้นทีละ 1 ในลูป จะเขียนอย่างไร?

<p><code>count += 1</code> (D)</p> Signup and view all the answers

พิจารณาโค้ดต่อไปนี้: x = 5; x = x + 5; x มีค่าเท่ากับเท่าไรหลังจากรันโค้ดนี้?

<p>10 (C)</p> Signup and view all the answers

ถ้าคุณต้องการรวมข้อความสองตัวแปร first_name และ last_name เข้าด้วยกันโดยมีช่องว่างคั่นกลาง ควรทำอย่างไร?

<p><code>first_name + &quot; &quot; + last_name</code> (C)</p> Signup and view all the answers

ถ้าคุณมีตัวแปร is_active = False และต้องการเปลี่ยนเป็น True ควรเขียนโค้ดอย่างไร?

<p>ทั้ง B และ C ถูก (A)</p> Signup and view all the answers

สมมติว่าคุณมีรายการตัวเลข numbers = [1, 2, 3, 4, 5] และต้องการเข้าถึงตัวเลข 3 คุณจะใช้ index อะไร?

<p>2 (D)</p> Signup and view all the answers

Flashcards

ตัวแปรคืออะไรใน Python?

ตัวแปรใช้เก็บข้อมูลที่จะถูกใช้งานในโปรแกรม

กำหนดค่าตัวแปรทำอย่างไร?

ใช้เครื่องหมาย = เพื่อกำหนดค่าให้กับตัวแปร

Integer คืออะไร?

ชนิดข้อมูลจำนวนเต็ม เช่น 1, 2, 100

Float คืออะไร?

ชนิดข้อมูลทศนิยม เช่น 1.0, 2.5

Signup and view all the flashcards

String คืออะไร?

ชนิดข้อมูลข้อความ เช่น 'Hello', 'Python'

Signup and view all the flashcards

Boolean คืออะไร?

ชนิดข้อมูลบูลีน เช่น True, False

Signup and view all the flashcards

ทำไมต้องใช้ตัวแปร?

ช่วยจัดการและจัดเก็บข้อมูลได้อย่างมีประสิทธิภาพ

Signup and view all the flashcards

Study Notes

ตัวแปรใน Python

  • ตัวแปรใน Python ใช้เพื่อเก็บข้อมูลที่โปรแกรมต้องการใช้
  • ตัวแปรทำหน้าที่เป็นตัวแทนที่สามารถเปลี่ยนแปลงค่าได้ระหว่างการทำงานของโปรแกรม

การกำหนดค่าตัวแปร

  • ใน Python ตัวแปรสามารถกำหนดค่าได้โดยใช้เครื่องหมาย =
  • ตัวอย่างการกำหนดตัวแปร:
    • x = 10
    • name = "Alice"
    • is_student = True

ชนิดของข้อมูลใน Python

  • Python มีชนิดข้อมูลที่ใช้บ่อยดังนี้:
    • จำนวนเต็ม (Integer): เช่น 1, 2, 100
    • ทศนิยม (Float): เช่น 1.0, 2.5
    • ข้อความ (String): เช่น "Hello", "Python"
    • บูลีน (Boolean): เช่น True, False

ตัวอย่างการใช้งานตัวแปร

  • การกำหนดค่าตัวแปร temperature เป็น 32 และ unit เป็น "Celsius" แล้วแสดงผลลัพธ์:
    temperature = 32
    unit = "Celsius"
    print(f"The temperature is {temperature} degrees {unit}.")
    
  • การเปลี่ยนค่าของตัวแปร score จาก 10 เป็น 20:
    score = 10
    score = 20
    
  • การใช้ตัวแปร name, age, และ is_student เพื่อแสดงผล:
    name = "John"
    age = 18
    is_student = True
    print(f"{name} is {age} years old and it is {is_student} that he is a student.")
    

แบบฝึกหัดที่ 1: การกำหนดตัวแปร

  • กำหนดตัวแปร age เป็นจำนวนเต็ม
  • กำหนดตัวแปร height เป็นทศนิยม
  • กำหนดตัวแปร first_name เป็นข้อความ
  • กำหนดตัวแปร is_teacher เป็นบูลีน

แบบฝึกหัดที่ 2: การเปลี่ยนแปลงค่า

  • เพิ่มค่า age อีก 5 ปี
  • ลดค่า height ลง 0.1 เมตร
  • เปลี่ยน first_name เป็นชื่อของคุณ
  • เปลี่ยน is_teacher เป็นค่าตรงข้าม

แบบฝึกหัดที่ 3: การใช้งานตัวแปร

  • เขียนโปรแกรมที่ใช้ตัวแปรเพื่อแสดงผลลัพธ์ในรูปแบบประโยค
  • ตัวอย่าง:
    age = 20
    height = 1.75
    first_name = "Alice"
    is_teacher = False
    print(f"{first_name} is {age} years old, {height} meters tall, and it is {is_teacher} that they are a teacher.")
    

คำถามเพิ่มเติม

  • การใช้ตัวแปรมีความสำคัญในการเขียนโปรแกรมเพราะช่วยให้จัดการและจัดเก็บข้อมูลได้อย่างมีประสิทธิภาพ
  • ความแตกต่างระหว่างชนิดข้อมูล Integer และ Float คือ Integer เก็บจำนวนเต็ม, Float เก็บเลขทศนิยม

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser