Monday, 10 November 2025

Python basic

 

Python Basics Summary

  1. Hello World Program

    • Use print("Hello, World!") to display text.
  2. Syntax Errors

    • Occur when Python can't understand your code due to incorrect structure.
    • Example: missing brackets, colons, or using wrong keywords.
    • print("Hello"  # ❌ SyntaxError: missing closing parenthesis

  3. Variable Naming Rules
    1. Must start with a letter or underscore (_)
    2. Can include letters, numbers, and underscores
    3. Cannot use Python keywords (like class, if, def)
    4. Case-sensitive (Namename)
    5. student_name = "Aisha"  # ✅ Valid
      1name = "Ben"           # ❌ Invalid
  4. Using Variables

    • Variables store data like text, numbers, or results of calculations.
    • name = "Aisha"
      score = 85
      total = score + 10
  5. Input from Command Prompt
    1. Use input() to get user input.
    2. Always returns a string, so convert to int or float if needed.
    3. name = input("Enter your name: ")
      age = int(input("Enter your age: "))
  6. Comments in Python

    • Used to explain code or leave notes.
    • Single-line: # This is a comment
    • Multi-line: ''' This is a multi-line comment '''

  7. Data Types
    1. str: text (e.g., "Hello")
    2. int: whole numbers (e.g., 10)
    3. float: decimal numbers (e.g., 3.14)
  8. Assignment Statements

    • Use = to assign values to variables.
  9. If-Else Statements

    • Make decisions using if, elif, and else.
    • score = 75
      if score >= 50:
          print("Pass")
      else:
          print("Fail")
  10. BIDMAS Rule in Arithmetic

    • Python follows Brackets, Indices, Division, Multiplication, Addition, Subtraction.
    • result = (3 + 2) * 4 ** 2 / 2 - 5
  11. Comparison Operators

    • ==, !=, >, <, >=, <= for comparing values.
  12. Indentation

    • Python uses indentation to define blocks of code (e.g., inside if, for, def).
  13. Types of Errors

    • Syntax Error: Code structure mistake
    • Runtime Error: Error during execution (e.g., divide by zero)
    • Logic Error: Code runs but gives wrong result
  14. Loops

    • for loop: repeat a known number of times
    • for i in range(5):
          print(i)
    • while loop: repeat while a condition is true
    • count = 1
      while count <= 5:
          print(count)
          count += 1
      

No comments:

Post a Comment

Python basic

  Python Basics Summary Hello World Program Use print("Hello, World!") to display text. Syntax Errors Occur when Pytho...