Introduction to Python Flashcards
Introduction to Python
What is Print?
print () - built-in function that displays input values as text in the output
What are Arithmetic operators?
Arithmetic Operators
+ Addition
- Subtraction
* Multiplication
/ Division
% Mod (the remainder after dividing)
** Exponentiation (note that ^ does not do this operation, as you might have seen in other languages)
// Divides and rounds down to the nearest integer
What is PEDMAS?
PEMDAS: Parentheses, Exponents, Multiplication/Division, Addition/Subtraction.
What is the type function?
it can be used to check the data type of any variable you are working with.
what is len() function?
len() is a built-in Python function that returns the length of an object, like a string. The length of a string is the number of characters in the string. This will always be an integer.
what are String Methods?
len(“this”)
type(12)
print(“Hello world”)
sample_string.lower()
my_string.islower() - does not accept another argument.
my_string.count(‘a’)
my_string.find(‘a’) - both take another argument
print(“Mohammed has {} balloons”.format(27)
print(“Does your {} {}?”.format(animal, action))
what is F-String Formatting?
F-strings provide a concise and convenient way to embed expressions inside string literals, using curly brackets {}. The expressions will be replaced with their values when the string is evaluated.
a = 5
b = 3
print(f”The sum of {a} and {b} is {a+b}”)
name = “John”
print(f”Hello, {name}”)
What is String Method - Split?
The split method has two arguments (sep and maxsplit).
The sep argument stands for “separator”. It can be used to identify how the string should be split up (e.g., whitespace characters like space, tab, return, newline; specific punctuation (e.g., comma, dashes)).
If the sep argument is not provided, the default separator is whitespace.
True to its name, the maxsplit argument provides the maximum number of splits. The argument gives maxsplit + 1 number of elements in the new list, with the remaining string being returned as the last element in the list.
Examples for split method
new_str = “The cow jumped over the moon.”
new_str.split()
[‘The’, ‘cow’, ‘jumped’, ‘over’, ‘the’, ‘moon.’]
new_str.split(‘ ‘, 3)
[‘The’, ‘cow’, ‘jumped’, ‘over the moon.’]
new_str.split(‘.’)
[‘The cow jumped over the moon’, ‘’]
new_str.split(None, 3)
[‘The’, ‘cow’, ‘jumped’, ‘over the moon.’]
How do you find the length, first, last and count occurrence of word from string?
length = len(verse)
first_idx = verse.find(‘and’)
last_idx = verse.rfind(‘you’)
count = verse.count(‘you’)
What Are Data Structures?
Data structures are containers or collections of data that organize and group data types together in different ways. You can think of data structures as file folders that have organized files of data inside them.
What are Lists?
list is one of the most common and basic data structures in Python.
How do you create List?
create a list with square brackets.
Lists can contain any mix and match of the data types you have seen so far.
list_of_random_things = [1, 3.4, ‘a string’, True]
Are lists Ordered?
Yes
Are lists Mutable?
Yes
Slice and Dice with Lists?
using slicing, it is important to remember that
the lower index is inclusive and
the upper index is exclusive
This type of indexing works exactly the same on strings, where the returned value will be a string.
What are Membership Operators?
in ==> evaluates if an element exists within our list
not in ==> evaluates if an element does not exist within our list
> > > 5 not in [1, 2, 3, 4, 6]
True
5 in [1, 2, 3, 4, 6]
False
Mutability?
Mutability refers to whether or not we can change an object once it has been created. If an object can be changed, it is called mutable. However, if an object cannot be changed after it has been created, then the object is considered immutable.
Examples - Lists are mutable, and strings are immutable.
Ordered?
Order is about whether the position of an element in the object can be used to access the element. Both strings and lists are ordered. We can use the order to access parts of a list and string.
Useful Functions for Lists?
len() returns how many elements are in a list.
max() returns the greatest element of the list.
max() function is undefined for lists that contain elements from different, incomparable types.
min() returns the smallest element in a list.
sorted() returns a copy of a list in order from smallest to largest, leaving the list unchanged.
append method
A helpful method called append adds an element to the end of a list.
What is join method in List
Join is a string method that takes a list of strings as an argument, and returns a string consisting of the list elements joined by a separator string.
new_str = “\n”.join([“fore”, “aft”, “starboard”, “port”])
print(new_str)
Output:
fore
aft
starboard
port
name = “-“.join([“García”, “O’Kelly”, “Davis”])
print(name)
Output:
García-O’Kelly-Davis
What are Tuples?
A tuple is another useful container. It’s a data type for immutable ordered sequences of elements. They are often used to store related pieces of information.
How Tuples can be created?
Tupes can be created using brackets ()
location = (13.4125, 103.866667)
The parentheses are optional when defining tuples, and programmers frequently omit them if parentheses don’t clarify the code.
dimensions = 52, 40, 100
How Tuples are accessed?
Tuples are similar to lists in that they store an ordered collection of objects which can be accessed by their indices.