Python Programming Fundamentals
24 Questions
2 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the purpose of the input() function in Python?

  • To display output on the screen.
  • To create a variable.
  • To take input from the user. (correct)
  • To read data from an external file.

Which of the following is used to comment out a line in Python?

  • --
  • /* */
  • //
  • # (correct)

Which method can be used to add an item to the end of a list in Python?

  • add()
  • append() (correct)
  • extend()
  • insert()

What will be the output of print(5 // 2)?

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

Which operator is used for comparison in Python?

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

Which of these is an immutable data type in Python?

<p>Tuple (B)</p> Signup and view all the answers

What does the break statement do in Python?

<p>Exits the current loop. (A)</p> Signup and view all the answers

How do you write a single-line comment in Python?

<h1>This is a comment (C)</h1> Signup and view all the answers

Which keyword is used to define a function in Python?

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

What will print(2 + 3 * 4) output?

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

Which Python method is used to find the index of an element in a list?

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

What will print(10 / 4) output?

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

Which of the following will create a list of numbers from 1 to 10 in Python?

<p>list(range(1, 11)) (C)</p> Signup and view all the answers

Which operator is used to repeat a string multiple times in Python?

<ul> <li>(D)</li> </ul> Signup and view all the answers

What does the enumerate() function do in Python?

<p>Returns an iterable of index-value pairs (B)</p> Signup and view all the answers

Which keyword is used to define an anonymous function in Python?

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

What does list.pop() do?

<p>Returns the last item and removes it from the list (C)</p> Signup and view all the answers

What will print("5" + "5") output?

<p>55 (B)</p> Signup and view all the answers

Which function is used to determine the type of an object in Python?

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

What is the output of print(10 // 3)?

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

What is the output of print(4 // 3)?

<p>1 (B)</p> Signup and view all the answers

Which of the following will check if a string starts with a specific prefix?

<p>startswith() (A)</p> Signup and view all the answers

How do you remove an element by its index from a list?

<p>list.pop(index) (A)</p> Signup and view all the answers

What is the purpose of the not keyword in Python?

<p>It negates the value of a boolean expression. (B)</p> Signup and view all the answers

Flashcards

What is a variable in Python?

A variable in Python holds data, like a container for information.

How to display "Hello, World!" in Python?

print("Hello, World!")

What is the input() function used for?

The input() function lets the user type in something and then you can use that input in your code.

How to create a list in Python?

A list is a collection of items in a specific order. You create a list by using square brackets and separating the items with commas.

Signup and view all the flashcards

Which operator is used for addition in Python?

The + operator in Python is used for adding numbers or combining strings.

Signup and view all the flashcards

What is a while loop in Python?

A while loop repeats a block of code as long as a certain condition is true.

Signup and view all the flashcards

How to comment out a line in Python?

The # symbol is used to comment out a single line of code. This means the commented-out code won't be executed.

Signup and view all the flashcards

How to create a function in Python?

A function is a block of code that performs a specific task and can be reused.

Signup and view all the flashcards

What is a string in Python?

A sequence of characters enclosed in single or double quotes.

Signup and view all the flashcards

How to handle exceptions in Python?

Use the try...except block to handle potential errors. The try block contains the code that might raise an exception. If an exception occurs, the program jumps to the except block, which handles the error gracefully.

Signup and view all the flashcards

What does the len() function do in Python?

The len() function returns the number of elements in a sequence, such as a string, list, or tuple.

Signup and view all the flashcards

How to create a sequence of numbers in Python?

Use the range() function! For example, range(5) generates numbers from 0 to 4. It's useful for looping through a sequence of numbers.

Signup and view all the flashcards

Checking for membership in a sequence in Python

The in operator checks if an element exists in a sequence, such as a list or string.

Signup and view all the flashcards

How to add an element to a list in Python?

Use the list.append(element) method to add an element to the end of a list.

Signup and view all the flashcards

What is a dictionary in Python?

A dictionary uses key-value pairs to store data. Keys are unique identifiers, and each key is associated with a value.

Signup and view all the flashcards

How to remove an item from a dictionary in Python?

Use the del dictionary[key] syntax to remove a key-value pair from a dictionary.

Signup and view all the flashcards

How do you convert a list to a set?

The set() function allows you to convert a list into a set. Sets are unordered collections of unique elements. Think of them as lists that automatically discard duplicates.

Signup and view all the flashcards

What does the not keyword do?

The not keyword in Python reverses the truth value of a Boolean expression. If a condition is True, not makes it False, and vice versa.

Signup and view all the flashcards

How to iterate through key-value pairs in a dictionary?

The items() function allows you to iterate over both keys and values of a dictionary. It returns a view object containing key-value pairs as tuples.

Signup and view all the flashcards

What does the enumerate() function do?

The enumerate() function helps you add a counter to an iterable (like a list). It returns an iterator where each item comes with its index as a tuple.

Signup and view all the flashcards

What keyword is used to create anonymous functions?

The lambda keyword is used to create anonymous functions in Python. These functions are nameless and can be defined in a single line.

Signup and view all the flashcards

Which data type is unordered in Python?

Dictionaries are unordered collections of key-value pairs. This means the order in which you add items to a dictionary might not be the same order in which they are retrieved.

Signup and view all the flashcards

What's the default value of a boolean?

In Python, a boolean value represents either True or False. The default value is False. It's like a light switch that's off by default.

Signup and view all the flashcards

How to open a file for writing?

The correct way to open a file for writing is to use the 'w' mode. This creates a new file or overwrites an existing file with the same name.

Signup and view all the flashcards

Study Notes

Python Programming Fundamentals

  • Variables: Used to store data values.
  • print() function: Displays output on the screen, using the syntax print("Hello, World!").
  • str data type: Stores sequences of characters.
  • input() function: Takes user input.
  • Lists: Ordered, mutable collections of items, created using square brackets: my_list = [1, 2, 3].
  • Operators: + for addition, - for subtraction, * for multiplication, / for division. Integer division (//) returns the whole number result. Modulo (%) returns the remainder. Exponentiation (**) raises a number to a power.
  • while loops: Used for repeated execution of code block, syntax: while x > 10:.
  • Comments: Used to explain code, using the # symbol.
  • Multi-line comments: Use triple quotes (single or double): """comment""".
  • append() method: Adds an item to the end of a list.
  • Data types:
    • int (integer)
    • float (floating-point number)
    • complex (complex number)
  • Classes: Define blueprints for creating objects. Syntax class MyClass:
  • try...except statement: For error handling, syntax try: and except:
  • len() function: Returns the length of a string or list.
  • tuple: Immutable (unchangeable) ordered collection of items. Created using parentheses. my_tuple = (1, 2, 3)
  • if statements: Control program flow, use conditional expressions. if x > 5:.
  • else statement: Executed if the if condition is false.
  • for loop: Used for looping over a sequence (list, tuple, string, etc.) or a range of numbers. syntax for x in range(5):. The range() function creates a sequence of numbers.
  • String methods: upper(), lower(), replace(), startswith(), endswith(), strip()
  • in operator: Checks for membership in a sequence.
  • not operator: Inverts the value of a boolean expression.
  • set: Unordered collection of unique items. Created using curly braces my_set = {1, 2, 3}
  • pop() method: Removes the item at a given index from the list and returns it.
  • clear() method: Removes all items from a list.

Studying That Suits You

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

Quiz Team

Related Documents

Python Questions PDF

Description

This quiz focuses on the foundational concepts of Python programming, including variables, data types, functions, and control structures. You will also learn about lists and operators, essential for effective coding. Test your knowledge and understanding of these basic yet crucial programming elements.

More Like This

Use Quizgecko on...
Browser
Browser