Codefinity Notebook - cPython
Codefinity Notebook - cPython
Variables can’t start with a number (Variables are the things you name strings/integers)
Variables are case sensitive
if : else:
[Indentation marks end of if/else] [only in VBA do you need endif endelse nextfor]
((str;; int, float, complex; list, tuple, range;; set;; bool;; dictionary;; bytes, binary))
‘for’-loops can work on strings, in which case it goes through each character
‘for’-loops in lists, obviously goes through each element
List.append(‘str’)
List.insert(0, ‘str’)
del List[0]
List.remove('call Max') <<< finds the string matching what you want to delete
manage = string.index('manage')
everything = string.index('everything')
burning = string.index('burning')
desire = string.index('desire')
studying = string.index('studying')
Tuples are basically Lists, but you can’t modify/edit them (they’re super efficient for memory/storage)
CONFUSING!!!:
dictionary = {“sf to la”: 500, “la to lv”: 400, “lv to nyc”: 3500}
for citypair, distancekm in dictionary.items():
distancekm[citypair] = dictionary / 0.621
total = sum(dictionary.values())
average = total / 3
True understanding will come from rewriting comments of line-code into plain-english
.count(‘string’)
.count(‘t’) <<<< counts occurences of “t” in total
.join
len( )
THEORETICAL ASSIGNMENT:
SPLIT AND INDEX AND REVERSE in multiple combinations
Step 3
for example: take a string
first half of sentence, reverse it, second half of sentence skip every 3 letters
take result of that, then skip every two words (not letters)
take result of that, and reverse word order
take result of that, remove last word
take result of that, reverse letter order (not words) ……
lazy_index = sentence.split('lazy') <<<< outputs the whole sentence with “lazy” blotted out
lazy_index = (sentence.split()).index('lazy')
lazy_index = sentence.split().index('lazy') both seem to work
By default, the print() function outputs each result on a new line. By employing the end=' ' argument, we ensure that
multiple print() outputs are separated by a space. We'll be using this technique throughout this section.
i=1
while i < 10: # Condition
print(i, end = ' ')
i=i+1
Lists work just like Slicing and Indexing
starts at 0, and the outer boundary is exclusive (not included)
Consider a two-dimensional list named countries_2d that contains 3 main elements (which are lists). Each of these lists
has 2 items. So, countries_2d[1] fetches the second list in the main list (keep in mind, Python indexing begins at 0).
Moreover, countries_2d[1][0] retrieves the first item within that second list.
# Two-dimensional list
countries_2d = [['USA', 9629091], ['Canada', 9984670], ['Germany', 357114]]
# Pull elements
print(countries_2d[1])
print(countries_2d[1][0])
>>
['Canada', 9984670]
Canada
As you've probably noticed, dictionaries have unique characteristics that set them apart from lists and tuples. They also
come with their own set of methods. Let's dive in.
d[k] = e - assigns the value e to the key k. If the key k already exists in the dictionary, its associated value will be updated.
!!!!!!!!!!
# Data
dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154),
'Germany': (357114, 83783942), 'Brazil': (8515767, 212559417),
'India': (3166391, 1380004385)}
# Defining a function
def function_giveinfo(x, n):
print('Country:', n)
print('Area:', x[n][0], 'sq km')
print('Population:', round(x[n][1]/1000000, 2), 'MM')
# Print symbols
print("This symbol was retrieved using negative indexation:", symbol1)
print("This symbol was retrieved using len() function:", symbol2)