Python Programming Tutorial
- 72Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What does it mean that Python is a general-purpose language?
72 questions
- 1
What does it mean that Python is a general-purpose language?
Model answer
It can be used to write software across many application domains rather than being restricted to one specific domain.
Explanation
The content contrasts general-purpose languages with domain-specific languages and lists Python's use in several fields.
- 2
What does it mean that Python is a high-level programming language?
Model answer
Python uses readable, natural-language-like elements and automates tasks such as memory allocation and management.
Explanation
These features make code easier to read and reduce the amount of low-level work programmers must handle.
- 3
How does an interpreted language differ from a language that is compiled into a machine-specific executable?
Model answer
Python scripts are executed in the format in which they are written, whereas compiled languages first translate code into machine-specific instructions and save an executable.
Explanation
This distinction describes how the content characterizes Python's execution model.
- 4
What does it mean that Python is dynamically typed?
Model answer
Python infers a variable's type from the value assigned to it, so the programmer does not have to declare the type explicitly.
Explanation
The parser identifies the type based on the assigned data.
- 5
Why can a Python variable be assigned values of different types without a type declaration?
Model answer
Python is dynamically typed, so it identifies the type from each assigned value rather than requiring a declared type.
Explanation
The content illustrates that Python infers types such as integer, string, and float from assigned data.
- 6
What is the difference between an interpreted language and a dynamically typed language?
Model answer
Interpretation describes how code is executed; dynamic typing describes how variable types are identified from their values.
Explanation
These terms refer to separate aspects of Python: execution and type handling.
- 7
How does Python's object-oriented paradigm organize programs?
Model answer
It allows program components to be grouped into individual objects, helping keep code organized and understandable.
Explanation
The content presents grouping and bundling code into objects as an advantage of object-oriented programming.
- 8
Why is Python described as cross-platform?
Model answer
A Python program written on one operating system, such as Windows, can run on another, such as Linux, with little or no modification.
Explanation
The content gives Windows and Linux as an example of Python's cross-platform compatibility.
- 9
What does it mean that Python is free and open source?
Model answer
Its source code is available to the public to download, use, modify, and distribute.
Explanation
These are the specific freedoms associated with Python's open-source status in the content.
- 10
How does Python's syntax benefit beginners?
Model answer
Its readable, English-like syntax makes code easier to understand and write, even for beginners.
Explanation
The content identifies ease of reading and learning as major reasons to choose Python.
- 11
How can Python's concise code reduce programming time?
Model answer
Python often requires fewer lines of code to express a task, so programmers can spend less time writing it.
Explanation
The content uses a one-line Python hello-world program as an example of concise code.
- 12
Why can Python's large library collection be useful to programmers?
Model answer
Libraries provide ready-made functionality and flexibility for Python users, and many are compatible across platforms.
Explanation
The content highlights Python's extensive library collection and its cross-platform compatibility.
- 13
Name three programming languages that Python can be integrated with.
Model answer
Python can be integrated with C, C++, and Java.
Explanation
The content names these languages as examples of Python integrations.
- 14
Why does the content recommend Python 3 over Python 2 to a new learner?
Model answer
Python 3 has stronger community support and continued development, while Python 2 is becoming obsolete and is not being developed to run faster.
Explanation
The content notes that companies were migrating from Python 2 to Python 3 and recommends beginners avoid spending time on an obsolete version.
- 15
What career-related advantages does the content associate with Python's range of applications?
Model answer
Python skills can support career opportunities across multiple domains, including data science, machine learning, web development, and automation.
Explanation
The content links Python's versatility and broad use to a range of career opportunities.
- 16
How is Python used in web development?
Model answer
Python can be used to create web applications with different levels of complexity.
Explanation
The content describes web application development as one of Python's applications.
- 17
Why is Python a preferred language for data analysis?
Model answer
Its many available libraries, including NumPy and pandas, support data analysis.
Explanation
The content connects Python's library availability with its preferred status for data analysis.
- 18
How can Python be used in machine learning?
Model answer
It can implement predictive analysis using machine-learning libraries such as scikit-learn and TensorFlow.
Explanation
The content identifies predictive analysis and these libraries as examples of Python's machine-learning use.
- 19
What Python resource does the content mention for game development?
Model answer
It mentions a game-development module called Pygame.
Explanation
Pygame is presented as a Python resource for creating games.
- 20
Give two examples of repetitive tasks that Python can automate.
Model answer
Python can automate tasks such as sending emails, opening a browser, or posting a Facebook status.
Explanation
The content gives these as examples of repetitive activities that Python can automate.
- 21
What annual salary figures does the content cite for data scientists, machine-learning developers, and Python developers?
Model answer
It cites about 127,826fordatascientists,143,650 for machine-learning developers, and $123,473 as the median for regular Python developers.
Explanation
These figures are the career salary estimates stated in the content.
- 22
Which Python version does the content recommend beginners download, and why?
Model answer
It recommends Python 3 because it is under active development, has stronger community support, and is increasingly replacing Python 2.
Explanation
The content advises beginners to avoid investing time in the increasingly obsolete Python 2.
- 23
What different environments can be used to write and run Python code?
Model answer
Python code can be run in the command-line interface, written in a text editor, or developed in an IDE such as Anaconda.
Explanation
The content names the CLI, text editors, and IDEs as available coding options.
- 24
What is a Jupyter Notebook, and what kind of environment does it provide?
Model answer
Jupyter Notebook is a web-based application for writing and running Python code, providing an interactive data-science environment.
Explanation
The content describes it as a browser-based tool for interactive Python work.
- 25
Why does the content recommend the Anaconda distribution for setting up Python and Jupyter Notebook?
Model answer
Anaconda includes Python, Jupyter Notebook, and other commonly used packages, simplifying the setup process.
Explanation
These bundled tools are the stated reason for recommending Anaconda.
- 26
How does indentation relate to Python development tools?
Model answer
Python relies heavily on indentation, and IDEs can help programmers write and debug code using it.
Explanation
The content specifically connects Python's indentation-based style with the support provided by IDEs.
- 27
What output would a Python environment produce for the expression 1+2?
Model answer
It produces 3.
Explanation
The content uses this expression to demonstrate how directly Python can perform a simple calculation.
- 28
What is a Python token?
Model answer
A token is a smallest meaningful component of Python source code.
Explanation
Tokens are basic components categorized according to their functions.
- 29
What four categories of tokens does the content identify?
Model answer
Keywords, literals, identifiers, and operators.
Explanation
These are the four token categories listed in the content.
- 30
What is a Python keyword, and why can it not be used as a variable name?
Model answer
A keyword is a reserved word with a specific purpose in Python, so it cannot be used as an identifier such as a variable name.
Explanation
Examples given include
for,if,break,continue,True, andFalse. - 31
What is an identifier in Python?
Model answer
An identifier is a name given to a variable, function, or class.
Explanation
Identifiers label named elements in a Python program.
- 32
Which special character is permitted within a Python identifier according to the content?
Model answer
The underscore is the only special character permitted.
Explanation
The content states that special characters other than the underscore cannot be used in identifiers.
- 33
Why is
A.Binvalid as a Python identifier whileA_Bis acceptable?Model answer
A period is not permitted in an identifier, but an underscore is.
Explanation
The underscore is the exception to the restriction on special characters.
- 34
Can a Python identifier begin with a digit? Explain.
Model answer
No. Its first character must be a letter or an underscore, not an integer.
Explanation
Although digits can appear in names after the first character, the content forbids starting an identifier with one.
- 35
How does case sensitivity affect Python identifiers?
Model answer
Identifiers with different capitalization are treated as different names; for example,
aandAare distinct.Explanation
The content demonstrates that assigning a value to uppercase
Adoes not change lowercasea. - 36
Why is
ifinvalid as a variable name?Model answer
ifis a reserved Python keyword and cannot be used as an identifier.Explanation
Keywords have predefined meanings in Python.
- 37
How can a programmer assign the same value to several variables at once?
Model answer
Use chained assignment, such as
a = b = c = 10.Explanation
Chained assignment assigns the same value to each named variable.
- 38
How can different values be assigned to several variables in one statement?
Model answer
List the variables and values in matching order, such as
x, y, z = 20, 30, 40.Explanation
This multiple assignment pairs each variable with its corresponding value.
- 39
What is a literal in Python?
Model answer
A literal is a constant value written directly in Python code, such as data assigned to a variable.
Explanation
The content describes literals as constants or data supplied in a variable.
- 40
What four categories of literals are described in the content?
Model answer
String, numeric, Boolean, and special literals.
Explanation
These are the four literal categories introduced in the content.
- 41
How can a string literal be written in Python?
Model answer
Enclose text in matching single or double quotation marks.
Explanation
The content shows that both single and double quotes can define a string.
- 42
How can a multiline string be written using the quoting method described in the content?
Model answer
Enclose the text in three matching quotation marks, allowing it to span multiple lines.
Explanation
Triple quotes preserve the string across the separate lines shown in the content.
- 43
What kinds of values can numeric literals represent?
Model answer
They can represent positive or negative numbers, with or without a decimal point.
Explanation
The content describes numeric literals as digit-based values that may have a sign and may include a decimal point.
- 44
How does the presence of a decimal point distinguish an integer literal from a floating-point literal?
Model answer
A numeric literal without a decimal point is an integer; one with a decimal point is a float.
Explanation
The content uses the decimal point to distinguish these numeric forms.
- 45
What constraint does the content give for decimal points in a numeric literal?
Model answer
A numeric literal can contain only one decimal point.
Explanation
The content explicitly notes that a number cannot have more than one decimal point.
- 46
How does Python handle the size of integer values?
Model answer
Integer values are not limited to a fixed width; they can grow up to the limits of available memory.
Explanation
The content states that Python's integer range depends on available memory.
- 47
What values can a Boolean literal have, and what are they used for?
Model answer
A Boolean literal is either
TrueorFalseand is used to represent the outcome of a condition.Explanation
The content describes Boolean values as results for conditions with two possible outcomes.
- 48
What is the purpose of the special literal
None?Model answer
Nonerepresents the absence of a value or a value that has not been created.Explanation
The content compares
Nonewith the concept ofnullin languages such as C, C++, and Java. - 49
Which types does the content classify as immutable, and which does it classify as mutable?
Model answer
It lists numbers, strings, and tuples as immutable, and lists, dictionaries, and sets as mutable.
Explanation
Immutable types cannot be changed or modified, while mutable types can be changed.
- 50
How can a programmer check the type of a Python value?
Model answer
Use the
type()function on the value or variable.Explanation
The content demonstrates
type()returning types such as integer, string, and float. - 51
What is the purpose of the arithmetic operators?
Model answer
They perform arithmetic calculations on operands, using operators such as addition, subtraction, multiplication, division, and modulo.
Explanation
The content describes arithmetic operators as characters that perform calculations on values.
- 52
What does an assignment operator do?
Model answer
It assigns a value to a variable.
Explanation
The equals sign in an assignment such as
var = 10is given as an example. - 53
What does the augmented assignment
var += 10mean?Model answer
It adds 10 to the current value of
varand assigns the result back tovar.Explanation
The content explains this shorthand as equivalent to
var = var + 10. - 54
What does the augmented assignment
a *= 10do?Model answer
It multiplies the current value of
aby 10 and stores the result ina.Explanation
The content identifies multiplication followed by assignment as an example of augmented assignment.
- 55
What do comparison operators return?
Model answer
They compare values and return either
TrueorFalse.Explanation
Examples include less than, greater than, equality comparisons, and not equal to.
- 56
What are Python's logical operators, and what do they evaluate?
Model answer
The logical operators are
and,or, andnot; they combine or invert Boolean conditions.Explanation
The content presents them as operators for logical calculations.
- 57
What is the result of
False and True, and why?Model answer
The result is
Falsebecause anandexpression is true only when both operands are true.Explanation
The content illustrates that false combined with true using
andevaluates to false. - 58
What do bitwise operators operate on?
Model answer
They perform operations on the binary representations of values.
Explanation
The content demonstrates bitwise operations by converting values to binary.
- 59
What are the results of the bitwise operations 7∣5 and 7&5?
Model answer
7∣5 equals 7, and 7&5 equals 5.
Explanation
In binary, 7 is 111 and 5 is 101; applying OR or AND bit by bit gives 111 or 101, respectively.
- 60
What does the bitwise NOT operator do to the bits of a value?
Model answer
It switches each bit from 0 to 1 or from 1 to 0.
Explanation
The content illustrates NOT by flipping the bits in the binary representation.
- 61
What result does the content give for the bitwise NOT of 7?
Model answer
It gives −8.
Explanation
The content shows the bit flip for 7 leading to a negative binary value equivalent to −8.
- 62
What happens when a binary value is shifted right by two positions?
Model answer
Its bits move two positions to the right; for example, shifting 10 right by two gives 2.
Explanation
The binary representation of 10, 1010, becomes 0010 after the shift.
- 63
What happens when a binary value is shifted left by two positions?
Model answer
Its bits move two positions to the left; for example, shifting 10 left by two gives 40.
Explanation
The binary representation of 10, 1010, becomes 101000, which is 40 in decimal.
- 64
What do the identity operators
isandis nottest?Model answer
They test whether two operands share identity or do not share identity.
Explanation
The content presents
isandis notas the two identity operators. - 65
What do the membership operators
inandnot intest?Model answer
They test whether a value is present in a sequence, such as a list, string, or tuple.
Explanation
The content illustrates membership by checking whether items occur in a list.
- 66
If a list contains
dog,cat, andwolf, what do membership tests forlionandwolfreturn?Model answer
lion inthe list returnsFalse, whilewolf inthe list returnsTrue.Explanation
Membership tests return true when the searched value occurs in the sequence and false when it does not.
- 67
How can a programmer access the first character of a string in Python?
Model answer
Use index 0; for example,
str1[0]accesses the first character.Explanation
The content demonstrates that string indexing begins at zero.
- 68
How can a programmer access the last character of a string using negative indexing?
Model answer
Use index −1; for example,
str2[-1]accesses the last character.Explanation
The content uses negative one to retrieve the final character.
- 69
How can a substring be extracted from a string using slicing?
Model answer
Use a start index and an end index, such as
str1[0:5]to extract the first five characters.Explanation
The starting position is included, but the ending position is excluded.
- 70
Why does the slice
str1[0:5]include characters at indices 0 through 4 but not index 5?Model answer
Python slices include the start index and exclude the end index.
Explanation
The content explains that the end position must be one beyond the final character wanted.
- 71
What does the string method
find()return?Model answer
It returns the position at which the specified substring begins.
Explanation
The content demonstrates
find()returning the starting index of a substring. - 72
What does the string method
replace()do?Model answer
It replaces a specified character or substring with another value.
Explanation
The content demonstrates replacing a substring with another string or with a space.

Ask anything
We've read this whole study set, so questions about it get the best answers.