官术网_书友最值得收藏!

Numbers

Numbers, as the name suggests, covers all the numeric data types, including both integer and floating data types. Earlier in this chapter, we saw that to use an integer or a float, we can simply declare the variable and assign an integer or a float value. Now, let's write a proper Python script and explore how to use numbers. Name the script numbers.py which is shown as follows:

The preceding screenshot show a simple Python script that adds an integer with a float and then prints the sum. To run the script, we can type the python3 numbers.py command, as follows:

You might have noticed that the command at the beginning of the script says #! /usr/bin/python. What this line does is make your code executable. After the privileges of the script have changed and it has been made executable, the command says that if an attempt is made to execute this script, then we should go ahead and execute it with python3, which is placed in the /usr/bin/python3 path. This can be seen in the following example:

If we observe the print command, we can see that the string formatter is %s. To fill it in with the actual value, the second argument to the print function is passed:

To convert a string into its equivalent integer or float value, we can use the built-in int() and float() functions.

主站蜘蛛池模板: 夏津县| 奉化市| 梁山县| 临夏市| 云阳县| 含山县| 缙云县| 读书| 大姚县| 永福县| 咸阳市| 本溪| 桂林市| 华宁县| 普陀区| 桐城市| 兴义市| 南城县| 庆云县| 马边| 海盐县| 图们市| 平远县| 紫云| 昌图县| 平和县| 广宗县| 墨脱县| 九龙城区| 大丰市| 邵武市| 长春市| 法库县| 铁岭市| 志丹县| 韶山市| 海门市| 东阿县| 鄂伦春自治旗| 新乡县| 灵丘县|