Headstart¶
Overview¶
The document demonstrate about the landscape of basic headstart for Python, started with constant, data types and structures.
Components¶
Constants¶
Following table that introduce the built-in constants supported
A small number of constants live in the built-in namespace. They are:
Constant | Description |
---|---|
False | The false value of the bool type |
True | The true value of the bool type |
None | An object frequently used to represent the absence of a value |
NotImplemented | Special value indicating that an operation is not implemented |
Ellipsis | an object frequently used to indicate that something is omitted. Aka of ... |
__debug__ | This constant is true if Python was not started with an -O option. |
Ellipsis | an object frequently used to indicate that something is omitted. Aka of ... |
Note:
. Assignments to False are illegal and raise a SyntaxError. . Assignments to True are illegal and raise a SyntaxError. . Assignments to None are illegal and raise a SyntaxError.
Assignment to Ellipsis is possible, but assignment to ... raises a SyntaxError. Ellipsis is the sole instance of the types.EllipsisType type.
Objects¶
Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects.
Data Types¶
Python has a rich set of built-in data types, which are illustrated in the diagram below.
Group | Type | Built-in (T|F) | Description |
---|---|---|---|
Numeric | int | True | |
float | True | ||
complex | True | ||
Boolean | bool | True | |
Text Sequence | str | True | |
Binary Sequence | bytes | True | |
bytearray | True | ||
memoryview | True | ||
Datetime | datetime | False, datetime | A combination of a date and a time |
date | False, datetime | A native date | |
time | False, datetime | An idealized time, independent of any particular day | |
timezone | False, datetime | A class that implements the tzinfo abstract base class as a fixed offset from the UTC | |
timedelta | False, datetime | A duration expressing the difference between two datetime or date, to microsecond resolution. | |
None | None | True | |
Sequence | list | True | |
tuple | True | ||
range | True | ||
Set | set | True | |
frozen | True | ||
Mapping | dict | True | |
Containers | namedtuple | False, colectition | named tuple factory function for creating tuple subclasses with named fields |
deque | False, colectition | list-like container with fast appends and pops on either end | |
ChainMap | False, colectition | dict-like class for creating a single view of multiple mappings | |
Counter | False, colectition | dict subclass for counting hashable objects | |
OrderedDict | False, colectition | dict subclass that remembers the order entries were added | |
defaultdict | False, colectition | dict subclass that calls a factory function to supply missing values | |
UserDict | False, colectition | wrapper around dictionary objects for easier dict subclassing | |
UserList | False, colectition | wrapper around list objects for easier list subclassing | |
UserString | False, colectition | wrapper around string objects for easier string subclassing |
Python provides a set of primitive data-types that do not rely on any other modules. |These include the numeric types, sequences, mappings, sets, and frozensets, as well as the None type and boolean types.
2.2 Core Native Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.3 Additional Container Data Types in the Standard Library . . . . . . . . . . . . . . . . . . . 24
from datetime import datetime, date
>>> 1699842778.2149107
1699842778.2149107
>>> a = 1699842778.2149107
>>> import datetime
>>> datetime.datetime.utcfromtimestamp(a)
datetime.datetime(2023, 11, 13, 2, 32, 58, 214911)
Text Data str
¶
The string is representation, you can see this in very form.
Methods | Description | Example |
---|---|---|
len() | Returns the length of the string | len("hello") |
upper() | Returns a string in uppercase | "hello".upper() |
lower() | Returns a string in lowercase | "hello".lower() |
strip() | Removes leading and trailing whitespace | " hello ".strip() |
lstrip() | Removes leading whitespace | " hello ".lstrip() |
rstrip() | Removes trailing whitespace | " hello ".rstrip() |
split() | Splits a string into a list of words | "hello world".split() |
join() | Joins a list of words into a string | " ".join(["hello", "world"]) |
replace() | Replaces a substring with another substring | "hello world".replace("hello", "goodbye") |
find() | Returns the index of the first occurrence of a substring | "hello world".find("world") |
index() | Returns the index of the first occurrence of a substring | "hello world".index("world") |
count() | Returns the number of occurrences of a substring | "hello world".count("o") |
startswith() | Returns True if the string starts with the given substring | "hello world".startswith("hello") |
endswith() | Returns True if the string ends with the given substring | "hello world".endswith("world") |
isalpha() | Returns True if all characters in the string are alphabets | "hello".isalpha() |
isalnum() | Returns True if all characters in the string are alphanumeric | "hello123".isalnum() |
isdecimal() | Returns True if all characters in the string are decimal characters | "123.45".isdecimal() |
isspace() | Returns True if all characters in the string are whitespace characters | " ".isspace() |
Iterator
Arguments
Results
Example
accumulate()
p [,func]
p0, p0+p1, p0+p1+p2, …
accumulate([1,2,3,4,5]) → 1 3 6 10 15
- The description of data
Working with number¶
Limitation of float or
The list and dict¶
Dictationry (dict
)¶
The dictionary is very
Transformation between data-types¶
Make new form¶
Package itertools
that support for efficient looping¶
The itertools
package provides a variety of functions to efficiently loop over iterable objects.
See more: Python3 - Itertools
Reference¶
- Python - Data Model