Getting started with Python
Kuo, Yao-Jen yaojenkuo@ntu.edu.tw from DATAINPOINT
Programming is a sequence of instructions that specifies how to perform a computation. The computation might be something mathematical, such as solving a system of equations, something symbolic, such as searching for and replacing text in a document, or something graphical, like processing an image.
Data analysis is a process of inspecting, cleaning, transforming, and modeling data with the goal of discovering useful information, informing conclusions, and supporting decision-making.
ks.plot_survey_summary(question_index="Q7", n=1)
ks.plot_survey_summary(question_index="Q8", n=1)
A standard library refers to any library where the latest version of the code is maintained and hosted by the official organization Python.org and comes with the installation of Python.
A third party library refers to any library where the latest version of the code is not maintained and hosted by neither ourselves nor the official organization Python.org.
As known as the "Notebook-based" solution or the "Jupyter ecosystem".
/tree
for classic Jupyter Notebook./lab
for Jupyter Lab.python --version
then hit Enter.print("Hello, world!")
Hello, world!
Long time Pythoneer Tim Peters) succinctly channels the BDFL's guiding principles for Python's design into 20 aphorisms, only 19 of which have been written down.
import this
The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
print()
in our previous example?¶print()
is one of the so-called built-in functions in Python.
A function is a named sequence of statements that performs a computation, either mathematical, symbolic, or graphical. When we define a function, we specify the name and the sequence of statements. Later, we can call the function by name.
A pre-defined function, we can call the function by name without defining it.
print()
help()
type()
help()
¶help(print)
Help on built-in function print in module builtins: print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.
help(type)
Help on class type in module builtins: class type(object) | type(object_or_name, bases, dict) | type(object) -> the object's type | type(name, bases, dict) -> a new type | | Methods defined here: | | __call__(self, /, *args, **kwargs) | Call self as a function. | | __delattr__(self, name, /) | Implement delattr(self, name). | | __dir__(self, /) | Specialized __dir__ implementation for types. | | __getattribute__(self, name, /) | Return getattr(self, name). | | __init__(self, /, *args, **kwargs) | Initialize self. See help(type(self)) for accurate signature. | | __instancecheck__(self, instance, /) | Check if an object is an instance. | | __repr__(self, /) | Return repr(self). | | __setattr__(self, name, value, /) | Implement setattr(self, name, value). | | __sizeof__(self, /) | Return memory consumption of the type object. | | __subclasscheck__(self, subclass, /) | Check if a class is a subclass. | | __subclasses__(self, /) | Return a list of immediate subclasses. | | mro(self, /) | Return a type's method resolution order. | | ---------------------------------------------------------------------- | Class methods defined here: | | __prepare__(...) | __prepare__() -> dict | used to create the namespace for the class statement | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) | Create and return a new object. See help(type) for accurate signature. | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __abstractmethods__ | | __dict__ | | __text_signature__ | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __base__ = <class 'object'> | The base class of the class hierarchy. | | When called, it accepts no arguments and returns a new featureless | instance that has no instance attributes and cannot be given any. | | | __bases__ = (<class 'object'>,) | | __basicsize__ = 880 | | __dictoffset__ = 264 | | __flags__ = 2148293632 | | __itemsize__ = 40 | | __mro__ = (<class 'type'>, <class 'object'>) | | __weakrefoffset__ = 368
help()
on help()
¶help(help)
Help on _Helper in module _sitebuiltins object: class _Helper(builtins.object) | Define the builtin 'help'. | | This is a wrapper around pydoc.help that provides a helpful message | when 'help' is typed at the Python interactive prompt. | | Calling help() at the Python prompt starts an interactive help session. | Calling help(thing) prints help for the python object 'thing'. | | Methods defined here: | | __call__(self, *args, **kwds) | Call self as a function. | | __repr__(self) | Return repr(self). | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined)
def
the name of our functionreturn
the output of our functiondef function_name(INPUTS: type, PARAMETERS: type, ...) -> type:
"""
docstring: print documentation when help() is called
"""
# sequence of statements
return OUTPUTS
def add(x: int, y: int) -> int:
"""
Equivalent to x + y
"""
return x + y
help(add)
Help on function add in module __main__: add(x: int, y: int) -> int Equivalent to x + y
add(5, 6)
11
+
, -
, *
, /
are quite straight-forward.**
for exponentiation.%
for remainder.//
for floor-divide.def from_fahrenheit_to_celsius(x: int) -> float:
out = (x - 32) * 5/9
return out
print(from_fahrenheit_to_celsius(32))
print(from_fahrenheit_to_celsius(212))
0.0 100.0
sorted()
function takes a bool
argument for reverse
parameter¶list_to_be_sorted = [11, 5, 7, 2, 3]
print(sorted(list_to_be_sorted, reverse=True))
print(sorted(list_to_be_sorted))
[11, 7, 5, 3, 2] [2, 3, 5, 7, 11]
function_name(OBJECT, ARGUMENTS) # function
OBJECT.method_name(ARGUMENTS) # method
list
has a method sort()
works like sorted()
function¶list_to_be_sorted = [11, 5, 7, 2, 3]
print(sorted(list_to_be_sorted))
list_to_be_sorted.sort()
print(list_to_be_sorted)
[2, 3, 5, 7, 11] [2, 3, 5, 7, 11]
list_to_be_sorted
being updated?¶# update through return
list_to_be_sorted = [11, 5, 7, 2, 3]
sorted_list = sorted(list_to_be_sorted)
print(sorted_list)
[2, 3, 5, 7, 11]
# update through change of state
list_to_be_sorted = [11, 5, 7, 2, 3]
list_to_be_sorted.sort()
print(list_to_be_sorted)
[2, 3, 5, 7, 11]