Untitled Deck Flashcards
What is Python?
A powerful, high-level programming language known for its readability and versatility
Python is an interpreted language, meaning code is executed line by line without needing to be compiled first.
What are the two primary modes of operation in Python?
- Interactive Mode
- Script Mode
Interactive Mode allows direct interaction with the Python interpreter, while Script Mode involves writing code in a file and executing it all at once.
List some applications of Python.
- GUI Apps
- Web Apps
- Games
- DBMS Apps
- Scripting
Python is widely used for developing various types of applications.
What is a limitation of Python regarding speed?
Considered slower compared to compiled languages like C++
This can impact performance in compute-intensive applications.
What are tokens in Python?
The smallest unit of a programming language, also known as a lexical unit
Tokens are the building blocks of Python.
Name the types of tokens in Python.
- Keywords
- Identifiers
- Literals
- Operators
- Punctuators
Each type of token serves a specific purpose in Python code.
What are keywords in Python?
Reserved words with specific meanings that cannot be used as identifiers
Keywords provide instructions to the interpreter.
Provide examples of Python keywords.
- if
- else
- for
- while
- def
- class
- import
- return
- try
- except
- finally
- with
- yield
- True
- False
- None
- and
- or
- not
- is
- in
- global
- nonlocal
- pass
- break
- continue
- lambda
- del
- raise
- assert
- async
- await
These keywords are fundamental to Python’s syntax and functionality.
What are identifiers in Python?
Names given to different parts of a program, such as variables, objects, classes, and functions
Identifiers must follow specific rules.
What are the rules for naming identifiers in Python?
- Must begin with a letter or an underscore
- Can include letters, numbers, and underscores
- Case-sensitive
- No limit on length
- Cannot use keywords
- Only underscores allowed for special symbols
These rules help maintain clarity and avoid conflicts in the code.
What are literals in Python?
Constant values that are directly embedded in the code
Literals can be of various types, including strings, numbers, and booleans.
List the types of literals in Python.
- String Literals
- Numeric Literals
- Boolean Literals
- Special Literal None
- Literal Collections
Each type serves a unique purpose in representing data.
What are arithmetic operators in Python?
Operators that perform mathematical operations
These include addition, subtraction, multiplication, and division.
What is the purpose of operators in Python?
Symbols that perform specific actions on operands
Operators are essential for manipulating data and performing calculations.
What are punctuators in Python?
Symbols used to structure and separate parts of the code
They ensure proper syntax and meaning in the code.
What is a data type?
Defines the kind of information a variable can hold
Data types help in managing the data and operations performed on it.
List the core data types in Python.
- Integer
- Floating Point
- Complex
- String
- List
- Tuple
- Dictionary
Each data type serves a specific role in data handling and storage.
What is Type Casting in Python?
Process of converting one data type to another
What are the two main types of type conversion in Python?
Implicit Type Conversion and Explicit Type Conversion
What is Implicit Type Conversion?
Automatic conversion by Python
What is Explicit Type Conversion?
Manual conversion by programmer
What does the int() function do?
Convert to integer
What does the float() function do?
Convert to floating-point number
What does the complex() function do?
Convert to complex number
What does the str() function do?
Convert to string
What does the bool() function do?
Convert to boolean
Provide an example of explicit type conversion.
a = “4”
b = int(a) # Explicit conversion from string to integer
What does the input() function return?
Always returns a string
Requires type casting for numerical operations
What is an empty statement in Python?
Uses pass keyword as a placeholder when no action is required
What is a simple statement?
Single line of code
Example: name = input(“Enter your Name”)
What is a compound statement?
Multiple statements grouped together, identified by a header line ending with a colon and an indented body
What are the types of control flow in Python?
Sequential, Selective, and Iterative
What is the syntax of an if statement?
if condition: # statements to execute
What is the syntax of an if-else statement?
if condition: # statements if true
else: # statements if false
What is a nested if-else?
Multiple levels of conditional checking for complex decision-making scenarios
What are the components of a loop?
Start, Step, and Stop
What are the types of loops in Python?
for loop and while loop
What does the range() function do?
Creates a sequence of numbers
Syntax: range(start, stop, step)
What is an example of the range() function?
range(0, 5) → [0, 1, 2, 3, 4]
What is another example of the range() function?
range(0, 10, 2) → [0, 2, 4, 6, 8]
What does the break statement do?
Exits loop immediately, useful for early termination
What does the continue statement do?
Skips current iteration and moves to next iteration
What does the in operator do?
Checks if item exists in sequence, returns True or False
What does the not in operator do?
Checks if item does not exist in sequence, returns opposite of in
How can strings be created in Python?
Direct Assignment or User Input
Example: name = “Python” or name = input(“Enter name: “)
What are the key takeaways about Python?
Python is flexible with type conversion, conditional and loop structures provide powerful control flow, understanding input handling is crucial, and membership operators simplify sequence checking.
What is string traversal?
The process of accessing each character in a string, one by one, for display or other purposes.
Example: Printing a string in reverse order.
What are the two string operators in Python?
- (Concatenation)
- (Replication)
+ joins two strings together, while * repeats a string a specified number of times.
What does the + operator do when used with strings?
Joins two strings together.
Example: ‘tea’ + ‘pot’ results in ‘teapot’.
What does the * operator do when used with strings?
Repeats a string a specified number of times.
Example: 5 * ‘@’ results in ‘@@@@@’.
What is string slicing?
A technique for extracting a portion of a string.
Syntax: word[start:stop]
How are indices assigned in a string?
Each character has an index starting from 0 for the first character and increasing by 1 for each subsequent character.
What is the reverse index in strings?
Starts from -1 for the last character and decreases by 1 for each preceding character.
What does the capitalize() function do?
Converts the first character of a string to uppercase.
What does the find() function return?
Returns the lowest index of a substring within a string.
What does the index() function return?
Returns the index of a substring within a string.
What does the isalnum() function check?
Checks if all characters in a string are alphanumeric (letters or numbers).
What does the isalpha() function check?
Checks if all characters in a string are alphabetical.
What does the isdigit() function check?
Checks if all characters in a string are digits.
What does the islower() function check?
Checks if all alphabetical characters in a string are lowercase.
What does the isupper() function check?
Checks if all alphabetical characters in a string are uppercase.
What does the join() function do?
Concatenates a sequence of strings into a single string, using a specified separator.
What does the lower() function do?
Converts all characters in a string to lowercase.
What does the upper() function do?
Converts all characters in a string to uppercase.
What does the len() function return?
Returns the length of a string (number of characters).
What does the ord() function return?
Returns the Unicode code point for a Unicode character.
What does the reversed() function return?
Returns a reversed iterator of a sequence.
What is the definition of a list in Python?
A standard data type that can store a sequence of values of any kind.
How are lists represented in Python?
Lists are enclosed in square brackets [ ].
What is meant by mutability in lists?
Lists are mutable data types, meaning their elements can be changed after creation.
What is the syntax for accessing an element in a list?
L[i] where i is the index of the element.
What does the + operator do when used with lists?
Concatenates two lists, adding the second list at the end of the first list.
What does the * operator do when used with lists?
Repeats a list a specified number of times.
What is the difference between strings and lists regarding mutability?
Strings are immutable; lists are mutable.
What is list traversal?
Accessing and processing each element of a list.
What is the syntax for list slicing?
seq = list[start:stop] where start is inclusive and stop is exclusive.
What happens when you modify a list using slicing?
You can replace elements of the list at specified indices.
Fill in the blank: The function _______ is used to create an empty list.
list()