Python
/
Collections
- 1 Language 9
-
Hello World S
-
Variables S
-
Functions S
-
Conditional A S
-
Operators S
-
While S
-
Turtle S
-
Script Mode S
-
Debugging S
- 2 Strings 7
-
Slice S
-
Raw Strings S
-
Regex A S
-
Validation S
-
Config S
-
Security S
-
Encrypt A S
- 3 Collections 5
-
Lists S
-
Dictionaries S
-
Efficiency S
-
Tuples S
-
References S
- 4 Functions 5
-
Recursion S
-
Factorial S
-
Modulus S
-
Reassignment S
-
Approximate S
- 5 Storage 8
-
Files S
-
Databases S
-
Pipes S
-
With open S
-
Shelve A S
-
Zip S
-
Csv S
-
Json S
- 6 Class 4
-
Definition S
-
Attributes S
-
Functional S
-
Methods S
- 7 Goodies 5
-
Conditional Expression S
-
List Comprehension A S
-
Generator S
-
Named Tuple S
-
Modules S
- 8 Applications 5
-
Pythagora A S
-
Palindrome A S
-
Binary Search A S
-
Conway Game A S
-
Coin Flip A S
- 9 Scheduler 4
-
Time S
-
Multithreading A S
-
Subprocess S
-
Logging S
- 10 Packages 6
-
Clipboard A S
-
Ocr A S
-
Socket S
-
Image S
-
Virtualenv S
-
Jupyter S
S
R
Q
Variable
p97 Variables are storing references to the computer memory locations.
"""Variables as references
When you assign a value to a variable, you are creating that value
in computer memory, and store a reference to it
When you assign new value to a, reference to a doesn't affect b
"""
a = 42; b = a; a = 100
assert a == 100
assert b == 42
print('Tests passed')
Lists
When you copy a list, you are copying the list (because lists are mutable).
""" References are different with lists
Lists are mutable
Even the code touches only A list ...
both A and B are changed!
Values stored in A and B both refer to the same list
"""
A = [1, 2, 3]; B = A; A[1] = 'x'
assert A == [1, 'x', 3]
assert B == [1, 'x', 3]
print('Test passed')
Garbage
p100 Python makes automatic garbage collection.
"""Python's automatic garbage collection
Delete any values not being referred to by any variables
Manual memory management in other programming languages ...
is a common source of bugs.
The getrefcount function returns the number of references
"""
import sys
a = 'somevalue'
assert sys.getrefcount(a) == 4
assert sys.getrefcount('somevalue') == 4
b = a; c = b
assert sys.getrefcount(a) == 6
del a; del b; del c
assert sys.getrefcount('somevalue') == 3 # Look Here
print('Tests passed')
➥ Questions
Last update: 117 days ago