Python Chapter 2

📘 Variables

  • A variable stores data in memory.
  • Example:name = "Alice" age = 15
  • Rules for variables:
    1. Must start with a letter or underscore (_)
    2. Cannot start with a number
    3. Case-sensitive (Name ≠ name)
    4. No spaces or special symbols
    5. Don’t use Python keywords

🔢 Data Types

TypeExampleDescription
int10Whole numbers
float3.14Decimal numbers
str“Hello”Text or characters
boolTrue / FalseLogical values
list[1, 2, 3]Collection of items
tuple(1, 2, 3)Ordered and unchangeable items
dict{“name”:”Ali”, “age”:15}Key-value pairs

💬 Input and Output

  • Input: input("Enter your name: ")
  • Output: print("Hello")
  • Input is always a string, use int() or float() to convert.

âž• Operators

🔹 Arithmetic Operators

+-*/%**//

🔹 Comparison Operators

==!=><>=<=

🔹 Logical Operators

andornot

🔹 Assignment Operators

=+=-=*=/=


🔣 Type Conversion

Change data type using:

int(), float(), str()

Leave a Comment

Your email address will not be published. Required fields are marked *