Q001-150 Flashcards
(150 cards)
What are the 2 rules for naming a variable?
- It can be only letters, numbers and underscores
- It can’t start with a number
What are the 2 ways of assigning multiple variables?
**a = b = c = 10** # all of them equal to 10
**a, b, c = 1, 2, 3** # each of them equals the respective number
What are the built-in data types? (15 types in 8 categories)
text type: str
numeric types: int, float, complex
sequence types: list, tuple, range
mapping type: dict
set types: set, frozen set
Boolean type: bool
binary types: byte, bytearray, memoryview
None type: NoneType
What are three features of an object in Python and which of them can be changed?
- ID - never changes
- type - never changes
- value - can change (mutable objects) or not (immutable objects)
Which data types are mutable and which immutable?
mutable: lists, byte arrays, sets and dictionaries
immutable: numeric data types, strings, bytes, frozen sets and tuples
How do we insert multi-line strings?
In between triple quotes with Enter after each line except of the last one:
my_string = ‘’’one
two
three’’‘
What’s the new line character and how to escape it?
\n
It’s a control character used to signify the end of a line of a text and start of a new one.
my_string = ‘’’one
two
three’’’
my_string = ‘one\ntwo\nthree’
****
A backslash has to be inserted at the end of a line (except from the last one)
my_string = ‘’’one\
two\
three’’’
my_string = ‘onetwothree’
What are the 2 ways of using indexes and slicing strings and their syntax?
- Going from the left - the first character has index 0, second 1 and so on
- Going from the right - the first character -1, second -2 and so on
syntax: string [start index : end index : step]
The end index is not included!
examples:
my_string = ‘unicode’
print (my_string[0])
print (my_string[-2])
print (my_string[1:3])
print (my_string[:3])
print (my_string[2:-2])
print (my_string[::2])
print (my_string[::-2])
u
d
ni
uni
ico
uioe
eoiu
How to find out the length of a string?
Using the len() function.
my_string = ‘unicode’
len(my_string)
7
What are the 3 basic operations with strings and what do they return?
x = ‘new’
y = ‘ order’
- Concatenation: x + y (returns string)
‘new order’
- Repetition: 3 * x (returns string)
‘newnewnew’
- Checking whether a character is part of it: ‘a’ in x or ‘a’ not in x (returns boolean)
False
True
What’s the syntax of formatting strings with % and {} operator and limiting decimal points of a float variable?
setting up the format structure:
x = ‘model: %s, %d slots, IOS %f’ % (2600XT, 2, 12.4)
print(x)
model: 2600XT, 2 slots, IOS 12.400000
or
x = ‘model: {}, {} slots, IOS {}’ .format (2600XT, 2, 12.4)
print(x)
model: 2600XT, 2 slots, IOS 12.4
changing values and limiting floating decimal points to 1:
y = ‘model: %s, %d slots, IOS %.1f’ % (2800XT, 4, 12.5)
print(x)
model: 2800XT, 4 slots, IOS 12.5
What is indexing in formatting strings with {} for and how to use it?
It’s useful when we want to switch the order of the values or repeat some of them:
x = ‘model: {1}, {2} slots, IOS {0}’ .format (2600XT, 2, 12.4)
print(x)
model: 2, 12.4 slots, IOS 2600XT
or
x = ‘model: {1}, {2} slots, IOS {1}’ .format (2600XT, 2, 12.4)
print(x)
model: 2, 12.4 slots, IOS 2
What is f-string formatting and what’s the syntax?
A way to embed variables, expressions and methods inside strings using a minimal syntax.
model = '2600XM' slots = 4 ios = 12.3
using variables, expressions and methods
f”Cisco model: {model.lower()}, {slots * 2} WAN slots, IOS {ios}”
‘Cisco model: 2600xm, 8 WAN slots, IOS 12.3’
What are the 2 important features of strings?
- They are immutable
- Upper and lower case matter
What are the 2 methods for finding a character within a string, their syntax, 2 important rules and what does it return?
Finding the first or the last occurence of a character or a substring of characters in a string or a substring starting and ending with given indexes:
x. find - the first occurence
x. rfind - the last occurence
syntax:
- *x.find(str, beg = 0, end = len(string))**
- *x.rfind(str, beg = 0, end = len(string))**
2 rules:
- when no indexes given, it searches in the whole string
- when given, the ending index is not include
Returns:
an integer if found, integer -1 if not found
What does string index method do, what’s the syntax and what does it return?
It is indexing the first or the last occurence of a character or a substring of characters in a string or a substring starting and ending with given indexes
x. index - the first occurence
x. rindex - the last occurence
syntax:
- *x.index(str, beg = 0, end = len(string))**
- *x.rindex(str, beg = 0, end = len(string))**
Returns:
an integer if found, raises an exception if not found
What’s the method for counting characters in a string, its syntax and return?
count()
Counting the number of occurences of a character or a substring of characters in a string or a substring starting and ending with given indexes
syntax:
x.count(str, beg = 0, end = len(string))
Returns:
an integer, integer 0 if not found
What are the methods for returning min and max character from a string and their syntax?
min() and max()
Returns:
a string (a lowercase character first, if there is none, then uppercase)
syntax:
min(str)
max(str)
example:
x = “The temptation of the internet”
min(x)
‘ ‘
max(x)
‘t’
What are the 5 methods for converting the case of a string and their syntax?
x.lower(), x.upper(), x.swapcase(), x.title() and x.capitalize
important! :
It’s not changing the original string, strings are immutable!
examples:
x = “The temptation of the internet”
x.lower()
‘the temptation of the internet’
x.upper()
‘THE TEMPTATION OF THE INTERNET’
x.swapcase()
‘tHE TEMPTATION OF THE INTERNET’
x.title()
‘The Temptation Of The Internet’
x = ‘tool’
x.capitalize()
‘Tool’
Which method can be used to replace or remove a character in a string?
x.replace(old, new, max number of occurences)
examples:
x = ‘ Remove the spaces ‘
x.replace(“ “, “”)
replaces spaces with no character:
‘Removethespaces’
x = ‘hahaha’
x.replace(‘a’,’e’)
‘hehehe’
x.replace(‘a’,’e’,2)
‘heheha’
Which method can be used to split a string and what’s the syntax and return?
x.split(delimiter, max number of delimiters)
= splitting a string by specifying a delimiter (space if not provided) with given maximum of delimiters (optional)
= returns a list
examples:
x = ‘one two three’
x.split(‘ ‘)
[‘one’, ‘two’, ‘three’]
x = ‘one two three’
x.split()
[‘one’, ‘two’, ‘three’]
x = ‘0111022203330’
x.split(‘0’)
creates an empty item of a list if the delimiter is at the beginning or the end:
[’’, ‘111’, ‘222’, ‘333’, ‘’]
x.split(‘ ‘,2)
[‘one’, ‘two’, ‘three’]
x.split(‘ ‘,1)
[‘one’, ‘two three’]
x.split(‘ ‘,0)
[‘one two three’]
Which method is used to split a string at new lines?
x.splitlines(keepends)
= splitting a string at all or given max of newlines (\n)
= when keepends set as True, line breaks are also included in items of the list
= returns a list
= if there are not line break characters, it returns a list with a single item
examples:
x = ‘One\nTwo\nThree\nFour’
x.splitlines()
[‘One’, ‘Two’, ‘Three’, ‘Four’]
x.splitlines(True)
[‘One\n’, ‘Two\n’, ‘Three\n’, ‘Four’]
Which method can be used to insert a character or a substring in between characters of a string?
join(str)
= inserting a character or a substring of characters in between every two characters
x = ‘Nova Dedina’
‘ ‘.join(x)
‘N o v a D e d i n a’
‘123’.join(x)
‘N123o123v123a123 123D123e123d123i123n123a’
024 What are the 3 methods for removing a character or a substring from the beginning and / or the end of a string?
str.strip(), str.lstrip() and str.rstrip()
= removing a character or a substring of characters from the beginning or/and the end of the string
= removes whitespaces if not specified (spaces, tabs, returns…)
examples:
x = ‘ Dude ‘
’ Dude\t\t\t’
x.strip()
‘Dude’
x = ‘aDudea’
x.strip(‘a’)
‘Dude’
x = ‘aaaDudeaaa’
x.strip(‘a’)
‘Dude’
x = ‘aaaDudea’
x.strip(‘a’)
‘Dude’
x = ‘abcDudeabc’
x.strip(‘abc’)
‘Dude’
x = ‘111Dude222’
only from the beginning of the string
x.lstrip(‘1’)
‘Dude222’
only from the end of the string
x.rstrip(‘2’)
‘111Dude’
leaves unchanged if not found
x.lstrip(‘2’)
‘111Dude222’