Podcast
Questions and Answers
What is the main difference between a tuple and a list in Python?
What is the main difference between a tuple and a list in Python?
Which operator has the highest precedence in Python?
Which operator has the highest precedence in Python?
Which of the following is a rule for valid variable names in Python?
Which of the following is a rule for valid variable names in Python?
What is the issue with the expression 0.1 + 0.2 - 0.3 in Python?
What is the issue with the expression 0.1 + 0.2 - 0.3 in Python?
Signup and view all the answers
How do you toggle line numbers in a Jupyter notebook?
How do you toggle line numbers in a Jupyter notebook?
Signup and view all the answers
What is the output of the expression (13 + 5) * 2
in Python?
What is the output of the expression (13 + 5) * 2
in Python?
Signup and view all the answers
In versions prior to Python 2.7, how did Python handle floating-point numbers?
In versions prior to Python 2.7, how did Python handle floating-point numbers?
Signup and view all the answers
Which of the following is NOT a free 'no install' option for running Python code?
Which of the following is NOT a free 'no install' option for running Python code?
Signup and view all the answers
What is the purpose of the append() method in a list?
What is the purpose of the append() method in a list?
Signup and view all the answers
How do you retrieve the number of times a specific value appears in a list?
How do you retrieve the number of times a specific value appears in a list?
Signup and view all the answers
Which method would you use to remove an item at a specific index from a list?
Which method would you use to remove an item at a specific index from a list?
Signup and view all the answers
When using the sort() method on a list, what is important to note?
When using the sort() method on a list, what is important to note?
Signup and view all the answers
What distinguishes dictionaries from lists?
What distinguishes dictionaries from lists?
Signup and view all the answers
How are lists defined in Python?
How are lists defined in Python?
Signup and view all the answers
What does the extend() method do in a list?
What does the extend() method do in a list?
Signup and view all the answers
Which method is used to reverse the elements of a list?
Which method is used to reverse the elements of a list?
Signup and view all the answers
What is a primary advantage of dynamic typing in Python?
What is a primary advantage of dynamic typing in Python?
Signup and view all the answers
Which of the following illustrates correct string slicing in Python?
Which of the following illustrates correct string slicing in Python?
Signup and view all the answers
Which of the following statements about variable assignments in Python is true?
Which of the following statements about variable assignments in Python is true?
Signup and view all the answers
What would be the output of the following code? print('The band for', name, 'is', band, 'out of 10')
What would be the output of the following code? print('The band for', name, 'is', band, 'out of 10')
Signup and view all the answers
What is the reverse index of the first character in the string 'hello'?
What is the reverse index of the first character in the string 'hello'?
Signup and view all the answers
Which of the following is NOT a valid way to format a string for printing in Python?
Which of the following is NOT a valid way to format a string for printing in Python?
Signup and view all the answers
What occurs if you mix single and double quotes incorrectly in a string?
What occurs if you mix single and double quotes incorrectly in a string?
Signup and view all the answers
What is the result of the operation mytax = myincome * tax if myincome is 1000 and tax is 0.2?
What is the result of the operation mytax = myincome * tax if myincome is 1000 and tax is 0.2?
Signup and view all the answers
What will happen if you try to add a duplicate value to a set?
What will happen if you try to add a duplicate value to a set?
Signup and view all the answers
Which method can be used to remove an element from a set without raising an error if the element does not exist?
Which method can be used to remove an element from a set without raising an error if the element does not exist?
Signup and view all the answers
What does the 'update' method do in the context of sets?
What does the 'update' method do in the context of sets?
Signup and view all the answers
How can you create a set containing unique characters from the string 'Mississippi'?
How can you create a set containing unique characters from the string 'Mississippi'?
Signup and view all the answers
What is the result of reading a file after the cursor has reached the end of the file?
What is the result of reading a file after the cursor has reached the end of the file?
Signup and view all the answers
Which of the following correctly identifies a property of sets?
Which of the following correctly identifies a property of sets?
Signup and view all the answers
What will the 'readlines' method return when called on a file object?
What will the 'readlines' method return when called on a file object?
Signup and view all the answers
Which statement correctly describes the outcome of calling the 'pop' method on a set?
Which statement correctly describes the outcome of calling the 'pop' method on a set?
Signup and view all the answers
What is the primary purpose of the myfile.seek(0)
function?
What is the primary purpose of the myfile.seek(0)
function?
Signup and view all the answers
What will the .readlines()
method return?
What will the .readlines()
method return?
Signup and view all the answers
Why is it important to use myfile.close()
after file operations?
Why is it important to use myfile.close()
after file operations?
Signup and view all the answers
What does the mode 'w' signify when opening a file?
What does the mode 'w' signify when opening a file?
Signup and view all the answers
Which of the following statements is true about the with
statement in file operations?
Which of the following statements is true about the with
statement in file operations?
Signup and view all the answers
What will happen if you try to open a file that does not exist using mode 'w'?
What will happen if you try to open a file that does not exist using mode 'w'?
Signup and view all the answers
What does the mode 'r+' allow you to do?
What does the mode 'r+' allow you to do?
Signup and view all the answers
How can you retrieve the last character from the string s = 'hello'
?
How can you retrieve the last character from the string s = 'hello'
?
Signup and view all the answers
What characteristic differentiates tuples from lists?
What characteristic differentiates tuples from lists?
Signup and view all the answers
Which method is used to add a new element to a dictionary?
Which method is used to add a new element to a dictionary?
Signup and view all the answers
Which of the following operations can be performed on a set?
Which of the following operations can be performed on a set?
Signup and view all the answers
How will the program respond if you attempt to reassign an element in a tuple?
How will the program respond if you attempt to reassign an element in a tuple?
Signup and view all the answers
What will the expression d.values()
return if d is defined as d={'K1':100,'K2':200,'K3':300}
?
What will the expression d.values()
return if d is defined as d={'K1':100,'K2':200,'K3':300}
?
Signup and view all the answers
What is the purpose of the method d.popitem()
in a dictionary?
What is the purpose of the method d.popitem()
in a dictionary?
Signup and view all the answers
Which method can be used to find the number of times a specific element appears in a tuple?
Which method can be used to find the number of times a specific element appears in a tuple?
Signup and view all the answers
Which of the following statements about sets is true?
Which of the following statements about sets is true?
Signup and view all the answers
Flashcards
Python Installation
Python Installation
Python can be installed using Anaconda from www.anaconda.com/downloads. Free online options like jupyter.org/try and Google Colab are alternative ways to use Python.
Jupyter Notebook - Keyboard Shortcuts
Jupyter Notebook - Keyboard Shortcuts
Jupyter Notebook uses keyboard shortcuts for common actions like toggling header or toolbar, and viewing line numbers.
Python - Adding Line Numbers
Python - Adding Line Numbers
Line numbers in code cells can be added via the Jupyter Notebook menu (View -> Toggle Line Numbers) or by using a keyboard shortcut (ESC then L).
Python Data Types - Tuples vs. Lists
Python Data Types - Tuples vs. Lists
Signup and view all the flashcards
Python Numbers - Integers vs Floats
Python Numbers - Integers vs Floats
Signup and view all the flashcards
Floating-Point Arithmetic Limitations
Floating-Point Arithmetic Limitations
Signup and view all the flashcards
Python Variable Names - Rules
Python Variable Names - Rules
Signup and view all the flashcards
Python Order of Precedence
Python Order of Precedence
Signup and view all the flashcards
Dynamic Typing
Dynamic Typing
Signup and view all the flashcards
Variable reassignment
Variable reassignment
Signup and view all the flashcards
String
String
Signup and view all the flashcards
String Indexing
String Indexing
Signup and view all the flashcards
String Slicing
String Slicing
Signup and view all the flashcards
String Interpolation
String Interpolation
Signup and view all the flashcards
Escape Sequences
Escape Sequences
Signup and view all the flashcards
String Length
String Length
Signup and view all the flashcards
List append()
List append()
Signup and view all the flashcards
List count()
List count()
Signup and view all the flashcards
List index
List index
Signup and view all the flashcards
List slicing
List slicing
Signup and view all the flashcards
List vs Dictionary
List vs Dictionary
Signup and view all the flashcards
Dictionary key-value
Dictionary key-value
Signup and view all the flashcards
Dictionaries
Dictionaries
Signup and view all the flashcards
Nested Lists
Nested Lists
Signup and view all the flashcards
What are Lists in Python?
What are Lists in Python?
Signup and view all the flashcards
What makes Lists unique?
What makes Lists unique?
Signup and view all the flashcards
Dictionaries in Python
Dictionaries in Python
Signup and view all the flashcards
Accessing values in a Dictionary
Accessing values in a Dictionary
Signup and view all the flashcards
Tuples in Python
Tuples in Python
Signup and view all the flashcards
Tuple Immutability
Tuple Immutability
Signup and view all the flashcards
Sets in Python
Sets in Python
Signup and view all the flashcards
Set Mutability
Set Mutability
Signup and view all the flashcards
Reset File Cursor
Reset File Cursor
Signup and view all the flashcards
Read Entire File
Read Entire File
Signup and view all the flashcards
Read Single Line
Read Single Line
Signup and view all the flashcards
Open File From Any Location
Open File From Any Location
Signup and view all the flashcards
Close File
Close File
Signup and view all the flashcards
Open File with 'with' Statement
Open File with 'with' Statement
Signup and view all the flashcards
File Modes
File Modes
Signup and view all the flashcards
File Mode 'w' (write)
File Mode 'w' (write)
Signup and view all the flashcards
How do you create a set?
How do you create a set?
Signup and view all the flashcards
What is add() for sets?
What is add() for sets?
Signup and view all the flashcards
What is remove() for sets?
What is remove() for sets?
Signup and view all the flashcards
What is a boolean?
What is a boolean?
Signup and view all the flashcards
What is the purpose of 'open()' for files?
What is the purpose of 'open()' for files?
Signup and view all the flashcards
What is the purpose of 'read()' for files?
What is the purpose of 'read()' for files?
Signup and view all the flashcards
What is the purpose of 'readline()' for files?
What is the purpose of 'readline()' for files?
Signup and view all the flashcards
Study Notes
Python Installation
- Download Anaconda from www.anaconda.com/downloads
- Free "No Install" options are available:
- jupyter.org/try
- Google Colab Online Notebooks
- Using free "No Install" options may present challenges in uploading user code, data, or notebooks
- File saving may not be possible in free versions
Jupyter Notebook
- Keyboard shortcuts are available through the Help menu
- Toggle header and toolbar can be accessed through the View menu
Add Number to Cells
- Use the menu, view, then toggle line number.
- Use the keyboard shortcut or press ESC then L.
Basic Data Types
- Integers: Whole numbers (e.g., 3, 300, 200) -
int
type - Floating-point: Numbers with a decimal point (e.g., 2.3, 4.6, 100.0) -
float
type - Strings: Ordered sequence of characters (e.g., "hello", 'Sammy', "2000"), or even emojis -
str
type - Lists: Ordered sequence of objects (e.g., [10,"hello",200.3]) -
list
type - Dictionaries: Unordered key-value pairs (e.g., {"mykey":"value","name": "Frankie"}) -
dict
type - Tuples: Ordered immutable sequence of objects (e.g., (10,"hello",200.3)) -
tup
type - Sets: Unordered collection of unique objects (e.g., {"a","b"}) -
set
type - Booleans: Logical values indicating True or False -
bool
type
Example Data Types
a=10
, type isint
b=10.5
, type isfloat
c="adel"
, type isstr
d=[10,20.5,"adel"]
, type islist
e={"K1":"vaue1", "k2": "Value2"}
, type isdict
f=(10,20.5,"adel")
, type istuple
s={"a","b",10,20.5}
, type isset
Order of Precedence in Python
- Parentheses, Exponential (exp), Multiplication, Division, Addition, Subtraction
- Multiple variable assignments in one line (e.g.,
a, b, c = 10, 20, 30
)
Basic Data Types: Tuples and Lists
- Tuples: Ordered, immutable sequences. Once an element is in a tuple, it cannot be changed or reassigned. Uses parentheses (e.g.,
(1,2,3)
). - Lists: Ordered, mutable sequences. Elements can be changed. Uses square brackets (e.g.,
[1,2,3]
).
Basic Data Types: Numbers
- Integers: Whole numbers (e.g., 3, 100)
- Floating-point: Numbers with decimal points (e.g., 3.14, 10.0)
Floating Point Arithmetic Issues
- Python's floating-point representation has limitations leading to inaccuracies in calculations involving decimal values (e.g., 0.1 + 0.2 ≠ 0.3).
Variable Assignments
- Variable names cannot start with numbers or contain spaces. Use underscores to separate words in variable names (e.g.,
my_dogs
). - Variables should typically be lowercase.
- Avoid Python keywords like
list
andstr
as variable names. - Python uses dynamic typing, meaning you can reassign a variable to a different data type.
Variable Assignments: Example Usage
myincome = 1000
tax = 0.2
mytax = myincome * tax
Print Multiple Variables
print("The band for", name, "is", band, "out of", 10)
Strings: Basic Concepts
- Strings are sequences of characters. Enclose strings in either single ('...') or double ("...") quotes.
- Indexing: Access individual characters (e.g.,
s[0]
for the first character) - Slicing: Extract subsets of characters (e.g.,
s[1:4]
to get characters from index 1 to 3)
Strings: Example Indices
- Characters: h -> e -> l -> l -> o
- Indices: 0 -> 1 -> 2 -> 3 -> 4
- Reverse Indices: -5 -> -4 -> -3 -> -2 -> -1
Strings: Escape Sequences
- '\n' (newline character) - starts a new line in a string print operation
- '\t' (tab character) - provides indentation in print operations
String Formatting
- String interpolation, using
print("Hello" + my_name)
, helps display data in strings.
String Indexing and Slicing: Summary
- Indexing grabs a single character.
- Slicing extracts a subsection of a string (e.g.,
slice[start:end:step]
).
String Operations: Mutability
- Strings are immutable. Attempts to assign a new value to a single index in a string will result in an error.
String Operations: Concatenation
- The "+" operator can be used to concatenate strings (e.g., 'Hello' + ' ' + 'World!')
String Operations: Functions
- Use
x.split('character')
to split a string based on a specific character
String Operations: Immutability
- Strings are immutable (cannot be changed after creation).
String Formatting for Printing
- Use
format()
method and f-strings:{variable}
in format stringsf'{variable}'
in f-strings
List Methods
append()
: Adds an element to the end of a list.insert()
: Inserts an element at a specific index.pop()
: Removes and returns the item at a specific index (or the last item if no index is given).remove()
: Removes the first occurrence of a specific item.sort()
: Sorts a list in place.sorted(mylist)
sorts a list and returns a new sorted list, without modifying the original.extend()
: Add multiple items to the end of a list.reverse()
: Reverses a list.
Dictionaries: Basic Concepts
- Dictionaries are unordered collections of key-value pairs.
- Use curly braces
{}
to define dictionaries, with keys and values separated by colons.
Dictionary Operations
- Access values by their keys:
my_dict['key1']
- Adding new key-value pair:
d['K4'] = 400
- Modifying an existing value:
d['K3'] = 5000
- Iterating over keys/values:
for k, v in my_dict.items():
keys()
,values()
,items()
methods return views.
Tuples: Summary
- Tuples are similar to lists but are immutable. Values inside tuples cannot be changed. Use parenthesis (
()
).
Sets: Basic Concepts
- Sets are unordered collections of unique elements.
- Use curly braces
{}
to create sets or use theset()
constructor to create a set from a list.
Set Methods
add()
: Adds an item to the set. If the item already exists, it's ignored.remove()
: Removes the specific item, resulting in an error if the item doesn't exist.pop()
: Removes and returns an arbitrary item from the set.discard()
: Removes the specific item if it exists. Otherwise, it does nothing. No error if the item doesn't exist.
Boolean: Summary
- Booleans represent truth values (True or False).
Math Operations
import math
math.pow(10, 2)
calculates 10 raised to the power of 2 (10 squared)
Files
- I/O (Input/Output): Reading and writing to files in python.
- Modes:
- read only (
'r'
) - open only to read from the file - write only (
'w'
) - overwrite the file or create a new one - append (
'a'
) - add to the file, without overwriting existing content - reading and writing (
'r+'
) - open to modify existing file - writing and reading (
'w+'
) - overwrite the file but read from file
- read only (
- Using
.read()
to read the entire file content - Using
.readline()
to read a single line from a file. - Using
.readlines()
read all lines (from file) into a list - Using
.seek(0)
to reset the cursor to the beginning of the file. - with statement:
- A useful way for opening and closing files, ensuring the file is always closed, and avoiding potential errors.
Assessment Test: Examples
- Strings: Find the square root of 100, reverse and print the string 'hello'.
- Lists: Build a list of three zeroes in two different ways, reassign a nested list element.
- Dictionaries: Access nested dictionary values.
- Tuples: Explain the difference between tuples and lists.
- Sets: Remove duplicates, Explain about set method and what they can do.
- Booleans: Explain boolean operators.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.