Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Notes on CS - python

The document covers various programming concepts including APIs, equality vs identity operators, exception handling in Python, and the use of f-strings. It also discusses data structures such as dictionaries, sets, and lists, along with their operations and applications. Additionally, it introduces type hinting in Python to improve code readability and maintainability.

Uploaded by

lindagreecesun
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Notes on CS - python

The document covers various programming concepts including APIs, equality vs identity operators, exception handling in Python, and the use of f-strings. It also discusses data structures such as dictionaries, sets, and lists, along with their operations and applications. Additionally, it introduces type hinting in Python to improve code readability and maintainability.

Uploaded by

lindagreecesun
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

CS NOTES:

APIs
Application programming interface

A way for different systems or applications to communicate with each other

EQUALITY VS IDENTITY

== is the equality operator.


'is' is the identity operator.

Equality Operator (==):

Purpose: Checks if the values of two objects are equal.


Usage: Use this operator when you want to compare the values stored in two
variables.

Identity Operator (is):

Purpose: Checks if two variables point to the same object in memory.


Usage: Use this operator when you want to check if two variables refer to the exact
same object (i.e., they have the same memory address).

CS EXCEPTIONS

Random little test

test = [1,2,3,4,5,6,8]

if 7 in test:
print('yes')
else:
print('no')

TRY and EXCEPT

x = int(input('what is x? '))
print(f'x is {x}') # the f and {}, allows you to insert a varaible without string
concatenation (f string)

This input only accepts integers, but will crash if say, a string was inputted. To
avoid this we use try and except

try:
x = int(input('what is x? '))
print(f'x is {x}')

except ValueError:
print('X is not an integer')

For good programming practices, you should only try code that will actually raise
an exception, so usually limit your 'try' to one line of code

try:
x = int(input('what is x? '))

except ValueError:
print('X is not an integer')

else:
print(f'x is {x}')

This is more efficient.

Putting it in a a loop:

while True:
try:
x = int(input('what is x? '))

except ValueError:
print('X is not an integer')

else:
break #you could also instead get rid of this and put the break after x =
int(input('what is x? '))

print(f'x is {x}')

while True:
try:
x = int(input('what is x? '))

except ValueError:
pass #pass can be used to not do anything with the condition. By using
it here, the loop just continues as it is still True

else:
break

print(f'x is {x}')
F STRINGS

# Python3 program introducing f-string


val = 'Geeks'
print(f"{val}for{val} is a portal for {val}.")

name = 'Om'
age = 22
print(f"Hello, My name is {name} and I'm {age} years old.")

LIBRARY MODULES

Random

import random #this imports everything from that module

coin = random.choice(['head','tails'])

print(coin)

This uses random.choice([seq])


This is where out of the two inputs added one is chosen at random. (In this case
either heads or tails is chosen at random

Using 'from' instead


Allows you to call a function just by its name

from random import choice #loads the function's name (choice) into the current
namespace

coin = choice(['heads','tails'])

random.shuffle(x)
import random

cards = ['jack','queen','king']

random.shuffle(cards)

for i in cards:
print(i)

Statistics
import statistics

print(statistics.mean([100,90]))

Packages - third party library

Iterating over multiple lists at the same time:

# Python program to iterate


# over 3 lists using zip function

import itertools

num = [1, 2, 3]
color = ['red', 'while', 'black']
value = [255, 256]

# iterates over 3 lists and executes


# 2 times as len(value)= 2 which is the
# minimum among all the three
for (a, b, c) in zip(num, color, value):
print (a, b, c)

use zip()

this also works

for item1, item2, item3 in zip((1, 2, 3), ('a', 'b', 'c'), (True, False, True)):
print(item1, item2, item3)

--------------------------------------------------

for (a, b, c) in itertools.zip_longest(num, color, value):


print (a, b, c)

same code but you must import itertools, and use itertools.zip_longest() to exhaust
all lists

for (a, b, c) in itertools.zip_longest(num, color, value, fillvalue=-1):


print (a, b, c)
the fillvalue fills a value for the 'none'

---------------------------------------------------

Another approach to iterate over multiple lists simultaneously is to use the


enumerate() function. The enumerate() function allows you to iterate over a list
and keep track of the current index of each element. You can use this index to
access the corresponding elements in the other lists.

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
list3 = ['x', 'y', 'z']

for i, element in enumerate(list1):


print(element, list2[i], list3[i])

where i is the index and element is the stuff in list1.

***********************************************************************************
***********************************

Iterating over nested lists:

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for sublist in list_of_lists:


for item in sublist:
print(item, end=' ')
print()

use a nested for loop

-------------------------------------------------------------
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

flattened_list = [item for sublist in nested_list for item in sublist]

print(flattened_list)

Or you can use list comprehensions where the nest list is flattened out

***********************************************************************************
*********************************

Accessing all elements at given list of indexes:

Using List Comprehension

# initializing lists
test_list = [9, 4, 5, 8, 10, 14]
index_list = [1, 3, 4]

# printing original lists


print ("Original list : " + str(test_list))
print ("Original index list : " + str(index_list))

# using list comprehension to


# elements from list
res_list = [test_list[i] for i in index_list]

# printing result
print ("Resultant list : " + str(res_list))

***********************************************************************************
***********************************

Check if elements exists in list in Python

super simple 'in'

lst=[ 1, 6, 3, 5, 3, 4 ]
#checking if element 7 is present
# in the given list or not
i=7
# if element present then return
# exist otherwise not exist
if i in lst:
print("exist")
else:
print("not exist")

***********************************************************************************
***********************************

# Python3 code to demonstrate working of


# Check if any element in list satisfies a condition
# Using list comprehension

# initializing list
test_list = [4, 5, 8, 9, 10, 17]

# printing list
print("The original list : " + str(test_list))

# Check if any element in list satisfies a condition


# Using list comprehension
res = True in (ele > 10 for ele in test_list) #List comprehension

# Printing result
print("Does any element satisfy specified condition ? : " + str(res))
CS LOOPING

print('meow \n'*3, end='') # by default, end = \n, which is why when you print
something new it prints to the next line, but by doing this, it means that the next
print will be on the same line.

DICTIONARIES
aka Hash Tables

dict - dictionary, allows you to associate one value to another


They allow you to use actual words as your indicies

students = {
'Hermoine': 'Gryffindor', where the names are the
'keys'
'Harry': 'Gryffindor',
'Ron': 'Gryffindor',
'Draco': 'Slytherin'

print(student['Hermione'])

Iterating through a dictionary should iterate through the keys

for student in students:


print(student, students[student], sep=',') #students[student] gets the
dictionary item e.g 'Gryffindor' for each key

students =[
{'Name': 'Hermione', 'House': 'Gryffindor', 'Patronus': 'Otter'},
{'Name': 'Harry', 'House': 'Gryffindor', 'Patronus': 'Stag'},
{'Name': 'Ron', 'House': 'Gryffindor', 'Patronus': 'Jack Russel Terrier'},
{'Name': 'Draco', 'House': 'Slytherin', 'Patronus': None} #The use of None,
clearly expresses that there intentinally is no value
]

for student in students:


print(student['Name'], student['House'], student['Patronus'], sep=',')

NESTED FOR LOOP

def main():
print_square(3)

def print_square(pSize):
#for each row in square
for i in range(pSize):

#for each brick in row


for j in range(pSize):

#print brick
print('#', end='')
print()

main()

Sets are data structures that are a collection of unique, unordered elements,
meaning that:
1. No duplicate elements
2. Elements cannot be accessed via index
3. Mutable - the set itself can be modified, but the elements within the set must
be immutable

Key features of a set ->


- automatic uniqueness
- unordered
- Fast membership testing (checking if an item exists) O(1)

BASIC OPERATIONS
# Creating a set
my_set = {1, 2, 3}
empty_set = set() # Note: {} creates a dictionary, not a set

my_set.add(4) # Adds 4 to the set


print(my_set) # Output: {1, 2, 3, 4}

my_set.remove(2) # Removes 2; throws an error if not found


my_set.discard(5) # Removes 5 if it exists; no error if it doesn't

Set Operations
Union (|): Combines all unique elements from both sets.
Intersection (&): Keeps only the elements common to both sets.
Difference (-): Keeps elements from the first set not in the second.
Symmetric Difference (^): Keeps elements that are in one set or the other, but not
both.

set_a = {1, 2, 3}
set_b = {3, 4, 5}

print(set_a | set_b) # Union: {1, 2, 3, 4, 5}


print(set_a & set_b) # Intersection: {3}
print(set_a - set_b) # Difference: {1, 2}
print(set_a ^ set_b) # Symmetric Difference: {1, 2, 4, 5}

Membership Testing:
print(3 in my_set) # True
print(5 not in my_set) # True

Applications of Sets:
Removing Duplicates: Quickly deduplicate items in a list:
my_list = [1, 2, 2, 3, 4, 4]
unique_elements = set(my_list) # {1, 2, 3, 4}#

Fast Membership Testing: Since sets are implemented as hash tables, lookups are
much faster than in lists.

Set Theory Operations: Useful in mathematics, databases, or situations requiring


union, intersection, or difference of data.

Data Filtering: Identify shared or distinct elements between datasets.

Set Limitations
Unordered: Not suitable when the order of elements matters.
Immutable Requirement: You cannot include mutable types like lists or dictionaries
as set elements.

string slicing

def isPalindrome(self, x: int) -> bool:


return str(x) == str(x)[::-1]

[::-1] -> reverses the string e.g 123 becomes 321

The ::, traverses through the whole list with the final number being the step

But if you want specific string slices for example the first two letters of apple,
the string slice would look like:

'apple'[0:2]

where the first num is the starting point and the second, the letters within the
range of the string regardless starting point
TYPE HINTING

Type hinting in Python is a feature that allows you to specify the expected data
types of variables, function parameters, and return values. It is primarily used to
improve code readability, maintainability, and to help with debugging and static
analysis by tools like type checkers (e.g., mypy). Type hints are optional and do
not affect the runtime behaviour of the program; they serve as a guide for
developers and tools.

Basic Variable Annotation


age: int = 25
name: str = "John"
is_student: bool = False

Function and parameter return type annotations


def greet(name: str) -> str:
return f"Hello, {name}!"

Using type hints with collections


from typing import List, Dict

def process_items(items: List[str]) -> None:


for item in items:
print(item)

def get_user_info(user_id: int) -> Dict[str, str]:


return {"name": "Alice", "id": str(user_id)}

Lists (arrays, records)


Linked Lists
Hash maps / dictionaries
Tuples
Sets -

-> what they are and when to use them


-> when reviweing all data structures and algorithms refer to above.

I shall be back once the WIFI signal improves, it is currently on holiday :/

sets - unique, cannot contain duplicates

You might also like