0% found this document useful (0 votes)
19 views29 pages

01 Additional Matrials

The document provides an overview of Python operators, variable naming conventions, and memory model concepts. It discusses different types of operators, the importance of descriptive variable names, and the role of literals and built-in functions in Python. Additionally, it touches on memory management, including the 'id' function and garbage collection, as well as the command line interface terminology.

Uploaded by

rmstn365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views29 pages

01 Additional Matrials

The document provides an overview of Python operators, variable naming conventions, and memory model concepts. It discusses different types of operators, the importance of descriptive variable names, and the role of literals and built-in functions in Python. Additionally, it touches on memory management, including the 'id' function and garbage collection, as well as the command line interface terminology.

Uploaded by

rmstn365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Abstraction, Hello Python, and

Memory Model
Additional Materials
Python Arithmetic Operators
Python Operators
• Python divides the operators in the following groups:

• Arithmetic operators: +, -, …
• Assignment operators: =, +=, -=, …
• Comparison operators: ==, >=, <=, > …
• Logical operators: and, or, not, …
• Identity operators: is, is not
• Membership operators, in
• Bitwise operators: &, |, ^, ~, <<. >>
https://www.w3schools.com/python/python_operators.asp
Python Operators
Arithmetic & Assignment

• balance = 1000
• deposit = 100
• balance = balance + deposit

• balance += deposit

Variable Names
(RealPython)

• Variables in Python are symbolic names pointing to objects or values in


memory.

• You de ne variables by assigning them a value using the assignment


operator.

• Python variables are dynamically typed, allowing type changes through


reassignment.

• Python variable names can include letters, digits, and underscores but can’t
start with a digit.

• Variables exist in di erent scopes


fi
ff
Variable Names (Convention)

https://en.wikipedia.org/wiki/Naming_convention_(programming)
Some coding styles

• “code is read much more often than it is written.”

https://www.threads.net/@donachodiary/post/C9PbGIuPtNV/media

https://peps.python.org/pep-0008/
Some coding styles

https://peps.python.org/pep-0008/
Good Variable Names

• use Descriptive Names (Intention-Revealing Names)


• Variable names should be nouns or noun phrases
• Prefer whole-word nouns to abbreviations.
• Use Pronounceable Names
• Remove Noise word (~ data, ~ value, ~object)
• Avoid names that are misleading or potentially confusing
• Avoid Magic Numbers
https://www.linkedin.com/pulse/clean-code-8-best-practices-naming-variable-shahryar-tayeb-znhpf/
Variable Names

• Avoid, 1, 0, l, O,
• ASCII compatibility
• ASCII stands for American
Standard Code for
Information Interchange.

• * Character Encoding
• UTF-8, Unicode, …
FYI, not python

https://dev.to/mohitsinghchauhan/clean-code-101-variable-naming-conventions-part-1-1c1a
FYI, not python
FYI, not python

https://dev.to/mohitsinghchauhan/clean-code-101-variable-naming-conventions-part-1-1c1a
FYI, not python
https://codingwithroby.substack.com/p/clean-code-the-art-of-clean-naming
Python Literals

• Literals in Python are xed values written directly in the code that represent
constant data.

• Numeric Literals (10, -25, -0.01, 5 + 2j , …)


• String Literals, (“ ”, ‘Sanghack Lee’, …)

• Boolean Literals (True, False)


• Collection Literals
• Check https://docs.python.org/3/library/stdtypes.html
https://www.geeksforgeeks.org/literals-in-python/
fi
Checking type
None Type

• Something related to “Nothing”


• Often used
• to de ne a variable (unassigned)
• to represent an empty state

• “Function” can return None


• function that returns nothing in fact returns None
• e.g. print function prints, it returns None
fi
Built-in Functions

• (I/O) input, print


• abs, pow, min, max, sum, round
• type, isinstance
• all, any,
• bool, bin, oct, hex, str, chr, ord, int
• list, tuple, dict,
• len, iter, range, sorted, zip
Python Errors

• Syntax Errors
• print(“abc)

• primp(“abc”)
• 1/0
• Bugs: A software bug is a design defect (bug) in computer software.
• Handling errors: e.g., timeout, internet connection, … 1/0 in calculator?
Memory Model

• “id” function returns the “identity” of an object.


• This is an integer which is guaranteed to be unique and
constant for this object during its lifetime.

• Two objects with non-overlapping lifetimes may have the


same id() value.

• CPython implementation detail: This is the address of the


object in memory.

• CPython is the reference implementation of the Python


programming language.
Memory Model (a bit more…)
id & “is” operator

• “is” operator compares “id”


Memory Model (a bit more…)
Library, Modules, Packages

• Python
• built-in types, functions, literals
• built-in modules
• The term library is simply a generic term for a bunch of code that was
designed with the aim of being usable by many applications.

• Module: A module is a le containing Python de nitions and statements.


• Package: Packages are a way of structuring Python’s module namespace
fi
fi
Garbage Collection
What is command line? terminal? shell?
• A command-line interface is any type of interface that is used to enter (textual) commands.
One of these is the terminal, but some programs have their own command-line interfaces.

• terminal = text input/output environment

• shell = command line interpreter


• dir or ls
dir & help

You might also like