☰
Python Across Disciplines
with Python + AI Tool   
×
Table of Contents

1.1.   Introduction 1.2.   About the Author & Contact Info 1.3.   Book Conventions 1.4.   What (Who) is a Programmer? 1.5.   Programming Across Disciplines 1.6.   Foundational Computing Concepts 1.7.   About Python 1.8.   First Steps 1.8.1 Computer Setup 1.8.2 Python print() Function 1.8.3 Comments
2.1. About Data 2.2. Data Types 2.3. Variables 2.4. User Input 2.5. Data Structures (DS)         2.5.1. DS Concepts         2.5.2. Lists         2.5.3. Dictionaries         2.5.4. Others 2.6. Files         2.6.1. Files & File Systems         2.6.2. Python File Object         2.6.3. Data Files 2.7. Databases
3.1. About Processing 3.2. Decisions         3.2.1 Decision Concepts         3.2.2 Conditions & Booleans         3.2.3 if Statements         3.2.4 if-else Statements         3.2.5 if-elif-else Statements         3.2.6 In-Line if Statements 3.3. Repetition (a.k.a. Loops)         3.3.1  Repetition Concepts         3.3.2  while Loops         3.3.3  for Loops         3.3.4  Nested Loops         3.3.5  Validating User Input 3.4. Functions         3.4.1  Function Concepts         3.4.2  Built-In Functions         3.4.3  Programmer Defined Functions 3.5. Libraries         3.5.1  Library Concepts         3.5.2  Standard Library         3.5.3  External Libraries 3.6. Processing Case Studies         3.6.1  Case Studies         3.6.2  Parsing Data
4.1. About Output 4.2. Advanced Printing 4.3. Data Visualization   4.4  Sound
  4.5  Graphics
  4.6  Video
  4.7  Web Output
  4.8  PDFs & Documents
  4.9  Dashboards
  4.10  Animation & Games
  4.11  Text to Speech

5.1 About Disciplines 5.2 Accounting 5.3 Architecture 5.4 Art 5.5 Artificial Intelligence (AI) 5.6 Autonomous Vehicles 5.7 Bioinformatics 5.8 Biology 5.9 Bitcoin 5.10 Blockchain 5.11 Business 5.12 Business Analytics 5.13 Chemistry 5.14 Communication 5.15 Computational Photography 5.16 Computer Science 5.17 Creative Writing 5.18 Cryptocurrency 5.19 Cultural Studies 5.20 Data Analytics 5.21 Data Engineering 5.22 Data Science 5.23 Data Visualization 5.24 Drone Piloting 5.25 Economics 5.26 Education 5.27 Engineering 5.28 English 5.29 Entrepreneurship 5.30 Environmental Studies 5.31 Exercise Science 5.32 Film 5.33 Finance 5.34 Gaming 5.35 Gender Studies 5.36 Genetics 5.37 Geography 5.38 Geology 5.39 Geospatial Analysis ☯ 5.40 History 5.41 Humanities 5.42 Information Systems 5.43 Languages 5.44 Law 5.45 Linguistics 5.46 Literature 5.47 Machine Learning 5.48 Management 5.49 Marketing 5.50 Mathematics 5.51 Medicine 5.52 Military 5.53 Model Railroading 5.54 Music 5.55 Natural Language Processing (NLP) 5.56 Network Analysis 5.57 Neural Networks 5.58 Neurology 5.59 Nursing 5.60 Pharmacology 5.61 Philosophy 5.62 Physiology 5.63 Politics 5.64 Psychiatry 5.65 Psychology 5.66 Real Estate 5.67 Recreation 5.68 Remote Control (RC) Vehicles 5.69 Rhetoric 5.70 Science 5.71 Sociology 5.72 Sports 5.73 Stock Trading 5.74 Text Mining 5.75 Weather 5.76 Writing
6.1. Databases         6.1.1 Overview of Databases         6.1.2 SQLite Databases         6.1.3 Querying a SQLite Database         6.1.4 CRUD Operations with SQLite         6.1.5 Connecting to Other Databases
Built-In Functions Conceptss Data Types Date & Time Format Codes Dictionary Methods Escape Sequences File Access Modes File Object Methods Python Keywords List Methods Operators Set Methods String Methods Tuple Methods Glossary Index Appendices   Software Install & Setup
  Coding Tools:
  A.  Python    B.  Google CoLaboratory    C.  Visual Studio Code    D.  PyCharm IDE    E.  Git    F.  GitHub 
  Database Tools:
  G.  SQLite Database    H.  MySQL Database 


Python Across Disciplines
by John Gordon © 2023

Table of Contents

Table of Contents  »  Chapter 3 : Processing : Functions : Built-In Functions

Built-In Functions

Subscribe Contact


Contents

Overview

Built-in functions are pre-defined modules included with the language and always available for use. Think of them as tools built into the Python language that you can use to perform specific tasks. Python has many built-in functions, such as print() for printing output to the console, len() for getting the length of a sequence like a string or list, and input() for accepting user input. These functions are always available and do not require additional code. Programmers can also create their functions in Python.

Documentation

There are over 50 built-in functions in Python. Rather than cover them all on this page, I will show you some of the commonly used built-in functions. For a complete list of the built-in functions and the official Python documentation for each, you can visit docs.python.org.

Examples

Function Description/Example
abs(x) Returns the absolute value of a number. The argument may be an integer, a floating point number, or an object implementing __abs__(). If the argument is a complex number, its magnitude is returned.

Example:
x = 5
y = -5
print(abs(x))
print(abs(y))
Output:
5
5
chr(i) Returns the string representing a character whose Unicode code point is the integer i.



Example: Use the ASCII table above for reference:
print(chr(35), end="")
print(chr(43), end="")
print(chr(53), end="")
print(chr(64), end="")
print(chr(65), end="")
print(chr(90), end="\n")
Output:
#+5@AZ
dir(object) Without arguments, returns the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.

Example:
x = 10
myList = [1,2,3,4,5,6,7,8,9,0]
print(dir())
Output:
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'myList', 'x']

format(value, format_spec='') Converts a value to a “formatted” representation, as controlled by format_spec. The interpretation of format_spec will depend on the type of the value argument. See the format_spec table below for a list of all the format specifiers.

Example:
name = "Alice"
age = 25
person = {"name": "Bob", "age": 30}
pi = 3.14159265359
print("My name is {} and I am {} years old.".format(name, age))
print("My name is {name} and I am {age} years old.".format(**person))
print("{0} is {1} years old and {0}'s favorite color is {2}.".format("Alice", 25, "blue"))
print("The {fruit} is {adjective}.".format(fruit="apple", adjective="delicious"))
print("Pi is approximately {:.2f}.".format(pi))
Output:
My name is Alice and I am 25 years old.
My name is Bob and I am 30 years old.
Alice is 25 years old and Alice's favorite color is blue.
The apple is delicious.
Pi is approximately 3.14.
format_spec Description
:<Left aligns the result (within the available space)
:>Right aligns the result (within the available space)
:^Center aligns the result (within the available space)
:=Places the sign to the left most position
:+Use a plus sign to indicate if the result is positive or negative
:-Use a minus sign for negative values only
: Use a space to insert an extra space before positive numbers (and a minus sign before negative numbers)
:,Use a comma as a thousand separator
:_Use a underscore as a thousand separator
:bBinary format
:cConverts the value into the corresponding unicode character
:dDecimal format
:eScientific format, with a lower case e
:EScientific format, with an upper case E
:fFix point number format
:FFix point number format, in uppercase format (show inf and nan as INF and NAN)
:gGeneral format
:GGeneral format (using a upper case E for scientific notations)
:oOctal format
:xHex format, lower case
:XHex format, upper case
:nNumber format
:%Percentage format
int(x=0) or (x, base=10) Returns an integer object constructed from a number or string x, or return 0 if no arguments are given.

Example:
stringNumber = "55"
print(int(stringNumber))
print(int(stringNumber) + 10)
Output:
55
65
len(s) Returns the length (the number of items) of an object.

Example:
lst = [33, 59, 10, 82, 44, 39]
print(len(lst))
strng = "United States of Ameria"
print(len(strng))
Output:
6
23
max(iterable, *, key=None) or (iterable, *, default, key=None) or (arg1, arg2, *args, key=None) Return the largest item in an iterable or the largest of two or more arguments.

Example:
lst = [33, 59, 10, 82, 44, 39]
print(max(lst))
Output:
82
min(iterable, *, key=None) or (iterable, *, default, key=None) or (arg1, arg2, *args, key=None) Return the smallest item in an iterable or the smallest of two or more arguments.

Example:
lst = [33, 59, 10, 82, 44, 39]
print(min(lst))
Output:
10
print(*objects, sep=' ', end='\n', file=None, flush=False) Print objects to the text stream file, separated by sep and followed by end. sep, end, file, and flush, if present, must be given as keyword arguments.

Example:
print("Name & SSN:")
print("Bob", end=" ")
print("Smith")
print("111","22","3333", sep="-")
Output:
Name & SSN:
Bob Smith
111-22-3333
str(object='') or (object=b'', encoding='utf-8', errors='strict') Returns a string version of the specified object.

Example:
pi = 3.14
print(pi)
print(str(pi))
print("The value of PI is " + str(pi))
Output:
3.14
3.14
The value of PI is 3.14
sum(iterable, /, start=0) Sums start and the items of an iterable from left to right and returns the total. The iterable’s items are normally numbers, and the start value is not allowed to be a string.

Example:
nums = [2,4,6,8,10]
print(sum(nums))
nums = (2,4,6,8,10)
print(sum(nums))

print(sum(nums,10))    # Add 10 to the sum of nums
Output:
30
30
40
type(object) or (name, bases, dict, **kwds) With one argument, return the type of an object. The return value is a type object and generally the same object as returned by object.__class__.

Example:
x = 10
y = 3.14
s = "Bob"
c = 'A'
lst = [1,2,3,4,5]
tup = (1,2,3,4,5)
print(type(x))
print(type(y))
print(type(s))
print(type(c))
print(type(lst))
print(type(tup))
Output:
< class 'int' >
< class 'float' >
< class 'str' >
< class 'str' >
< class 'list' >
< class 'tuple' >


 





© 2023 John Gordon
Cascade Street Publishing, LLC