Python -- Built in functions

Today, we cover a handful of the built-in functions with Python 3. 

For a full list, see: https://docs.python.org/3/library/functions.html

We cover absolute value (abs()), the help() functions, max(), min() ...which are how to find maximum and minimum of a list, how to round a number with round(), as well as ceil() and floor(), even though these last two are NOT built in, it just seemed like a good time to bring them up. Finally, we cover converting floats, ints, and strings to and from each other.

There are still quite a few other built in functions to Python 3, but the others are not really meant for a basics tutorial.


Sample code for the built in functions that are covered in the video:

Absolute Values:

exNum1 = -5
exNum2 = 5
print(abs(exNum1))
if abs(exNum1) == exNum2:
    print('True!')


The Help function:

This is probably one of the most under-utilized commands in Python, many people do not even know that it exists. With help(), you can type it with empty parameters to engage in a search, or you can put a specific function in question in the parameter.

help()
Or...
import time
# will work in a typical installation of Python, but not in the embedded editor
help(time)


Max and Min:

How to find the maximum or highest number in a list...

or how to find the lowest or minimum number in a list.

exList = [5,2,1,6,7]

largest = max(exList)
print(largest)

smallest = min(exList)
print(smallest)


Rounding:

Rounding will round to the nearest whole. There are also ways to round up or round down.

x = 5.622
x = round(x)
print(x)

y = 5.256
y = round(y)
print(y)


Converting data types:

Many times, like reading data in from a file, you might find the datatype is incorrect, like when we mean to have integers, but they are currently in string form, or visa versa.

Converting a string to an integer:

intMe = '55'
intMe = int(intMe)
print(intMe)

Converting and integer to a string:

stringMe = 55
stringMe = str(stringMe)
print(stringMe)


Converting an integer to a float:

floatMe = 55
floatMe = float(floatMe)
print(floatMe)

You can also convert floats to strings, strings to floats, and more. Just make sure you do a valid operation. You still cannot convert the letter h to a float.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值