Mighty Python

  1. How do you create a variable in Python?
    Assign a value to a variable name with the = symbol, like: x = 10.
  2. What symbol is used for comments in Python?
    The # symbol is used for single-line comments.
  3. What is a string in Python?
    A sequence of characters enclosed in quotes, like "Hello" or 'Python'.
  4. How do you get user input in Python?
    Use the input() function, like: name = input("Enter your name: ").
  5. What are the basic data types in Python?
    Common types include integers (int), floating-point numbers (float), strings (str), and booleans (bool).
  6. How do you create a list in Python?
    Use square brackets: my_list = [1, 2, 3].
  7. How do you access the first element of a list?
    Use indexing: my_list[0].
  8. What is a function in Python?
    A block of reusable code that performs a specific task. Defined using the def keyword.
  9. How do you define a function in Python?
    Use def, the function name, and parentheses: def my_function():.
  10. What does if do in Python?
    if checks a condition and executes code if the condition is true.
  11. How do you check if x is greater than 10 in Python?
    Use an if statement: if x > 10:.
  12. What is a loop in Python?
    A way to repeat a block of code multiple times. Common loops are for and while.
  13. How do you create a for loop that prints numbers 1 to 5?
    Use the range function: for i in range(1, 6): print(i).
  14. How do you create a while loop in Python?
    Use while and a condition: while x < 5: print(x); x += 1.
  15. What does len() do in Python?
    It returns the length of an object, like a list or string.
  16. How do you import a module in Python?
    Use the import keyword: import math.
  17. What is the purpose of return in a function?
    It sends a value back from a function to the caller.
  18. How do you handle errors in Python?
    Use try and except blocks to catch and handle exceptions.
Author
KeithD
ID
366219
Card Set
Mighty Python
Description
Updated