Python Programming Concepts

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/79

flashcard set

Earn XP

Description and Tags

A comprehensive set of flashcards covering key Python programming concepts, syntax, and error handling.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No study sessions yet.

80 Terms

1
New cards

What data type does the variable 'name' represent in Python?

String (str)

2
New cards

How do you convert a string '42' to an integer in Python?

Use int(x) where x is the string.

3
New cards

What is the result of bool(0) in Python?

False

4
New cards

How do you add an element to a list in Python?

Use the append method: list.append(element)

5
New cards

What is the order of operations precedence for the exponentiation operator?

It has the highest precedence.

6
New cards

What does the statement 'nums.sort()' do?

It sorts the list 'nums' in place.

7
New cards

What will the expression 2 ** 3 return?

8 (exponentiation result)

8
New cards

What operator is used for floor division in Python?

// (double slashes)

9
New cards

What is the last character of the string 'Python'?

The last character is 'n'.

10
New cards

In Python, what does the keyword 'None' represent?

It represents the absence of a value.

11
New cards

What is the default return value of a function in Python if no return statement is provided?

None

12
New cards

What does the 'break' statement do in a loop?

It exits the loop immediately.

13
New cards

How do you read an entire file as a single string in Python?

Use f.read() after opening the file.

14
New cards

What is the purpose of the 'with' statement when handling files in Python?

It ensures that the file is properly closed after its suite finishes.

15
New cards

What does the method f.write() do?

It writes a string to the opened file.

16
New cards

What is the output of print(f'The value of x is {x}')?

It interpolates the value of x within the f-string.

17
New cards

What error is raised when trying to access an out-of-range index of a list?

IndexError

18
New cards

What is the difference between '==' and 'is' in Python?

'==' checks value equality, while 'is' checks identity (same object).

19
New cards

What keyword is used to define a function in Python?

def

20
New cards

How can you skip the current iteration of a loop?

Use the 'continue' statement.

21
New cards

What module is used for unit testing in Python?

unittest

22
New cards

How do you handle exceptions in Python?

Use try and except blocks.

23
New cards

What type of error is raised when you try to divide by zero?

ZeroDivisionError

24
New cards

Which Python statement allows you to handle multiple exceptions?

You can use multiple except blocks after a single try block.

25
New cards

How do you check if a file exists in the filesystem?

Use os.path.exists('filename').

26
New cards

What does the 'import math' statement do?

It imports the math module, which provides mathematical functions.

27
New cards

Which operator is used for modulo operation to find the remainder?

% (percent sign).

28
New cards

What does the function math.sqrt(x) return?

The square root of x.

29
New cards

How would you format a string with replacement fields in Python?

Use str.format() or f-strings.

30
New cards

What function would you use to generate a random integer between 1 and 10?

random.randint(1, 10)

31
New cards

How can you append to a file using Python?

Use 'open(filename, 'a')' to open a file in append mode.

32
New cards

What is the use of the 'pass' statement?

It acts as a placeholder for future code.

33
New cards

How do you start a for loop that iterates over a list in Python?

Use for item in list: syntax.

34
New cards

How do you open a file for reading?

Use open('filename', 'r').

35
New cards

What method is used to strip newline characters from strings?

strip() method.

36
New cards

What does 'math.factorial(n)' compute?

The factorial of n.

37
New cards

What is the difference between a tuple and a list in Python?

Tuples are immutable; lists are mutable.

38
New cards

How do you define a class in Python?

Use the class keyword, e.g., class MyClass:.

39
New cards

What is the purpose of docstrings in Python?

To document code by describing functions or classes.

40
New cards

What happens if you miss a colon at the end of a function definition?

A SyntaxError will occur.

41
New cards

How do you check if a variable is an instance of a particular type?

Use isinstance(variable, type).

42
New cards

What is the output of print('Hello, World!')?

It prints 'Hello, World!' to the console.

43
New cards

What does the math.ceil(x) function return?

The smallest integer greater than or equal to x.

44
New cards

How can you create a random float in the range 0.0 to 1.0?

Use random.random().

45
New cards

What does the os.listdir('.') function do?

Lists all files in the current directory.

46
New cards

How do you specify a default value for a function parameter?

By assigning a value in the function definition: def func(param=default).

47
New cards

What function would you use to generate a random sample of elements from a list?

random.sample() method.

48
New cards

What does the term 'mutable' mean in Python?

An object whose content can be changed.

49
New cards

How do you create a loop that continues indefinitely?

Use a while True: loop.

50
New cards

What is the output of the expression 10 % 3?

1 (the remainder of the division).

51
New cards

How do you comment a single line in Python?

Use the '#' symbol.

52
New cards

What does the write() method do when used on an opened file object?

It writes the specified string to the file.

53
New cards

How do you check if a variable is None?

Use the 'is' operator: variable is None.

54
New cards

What does 'sys.argv' contain?

A list of command-line arguments passed to a Python script.

55
New cards

What is the purpose of the 'finally' clause in try-except blocks?

It is executed regardless of whether an exception occurred.

56
New cards

What is an AssertionError in Python?

It is raised when an assert statement fails.

57
New cards

How do you generate a random number from a given range?

Use random.randrange(start, stop, step).

58
New cards

What is a dictionary in Python?

A collection of key-value pairs.

59
New cards

How do you define a method within a class?

By defining it with 'def' inside the class.

60
New cards

What does math.frexp(x) return?

It returns the mantissa and exponent of x.

61
New cards

How are parameters specified in a function definition?

In parentheses following the function name.

62
New cards

What is a lambda function?

An anonymous function defined with the 'lambda' keyword.

63
New cards

What exception does accessing an undefined variable raise?

NameError.

64
New cards

What function is used to read a file line by line?

Read in a loop or use f.readlines() for all lines.

65
New cards

What is 'nested loops'?

A loop inside another loop.

66
New cards

What is the output of print(0 == False)?

True, since 0 is considered falsy.

67
New cards

What is the result of 5 * '2' in Python?

TypeError, cannot multiply a string by an integer.

68
New cards

What does 'math.isnan()' check for?

It checks if a value is NaN (Not a Number).

69
New cards

How do you remove an item from a list?

Use list.remove(item).

70
New cards

What is the symbol for exponentiation in Python?

** (double asterisks).

71
New cards

How can you check if an exception is raised?

Use a try-except block.

72
New cards

What results in a LogicError?

When the code runs but produces incorrect results.

73
New cards

How do you use loops to repeat code a certain number of times?

Use for or while loops.

74
New cards

How do you raise an exception in Python?

Use the 'raise' keyword.

75
New cards

What does a method in Python return if no return statement is present?

None.

76
New cards

What does 'with open(filename, 'r')' achieve?

It opens a file for reading in a context manager.

77
New cards

What is the function of the 'continue' statement?

It skips the current iteration and moves to the next.

78
New cards

What is the effect of a SyntaxError?

It prevents code from running due to incorrect syntax.

79
New cards

How do you get the length of a list in Python?

Use len(list).

80
New cards

What will 'name = input()' always return?

A string, regardless of input.