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

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.

主站蜘蛛池模板: 山阴县| 无极县| 广德县| 巴东县| 蓝山县| 浑源县| 陕西省| 长治县| 西充县| 潞城市| 临夏市| 嘉鱼县| 互助| 垦利县| 南木林县| 云龙县| 江川县| 永胜县| 六盘水市| 安阳县| 韩城市| 德阳市| 衡阳市| 吴堡县| 明光市| 嘉义县| 江源县| 汽车| 额敏县| 白银市| 东丽区| 黄龙县| 平果县| 江北区| 黎城县| 石嘴山市| 中江县| 高雄县| 沁源县| 宁安市| 绍兴县|