Python provides three main numeric types for handling different kinds of numeric data:• int. This data type is used for storing integer numbers — numbers that do not have a decimal component. For example, 0, 3, 42, and 4817 are all integers. Technically, the bool data type for storing Boolean values is a subtype of int.• float. This data type is used for storing floating‐point numbers, those that...
Friday, August 26, 2022
Wednesday, August 24, 2022
Understanding Python’s Data Types
Python includes various built‐in data types designed for handling different types of data efficiently. For example, Python’s bool data type is designed for storing Boolean data, data that can be either True or False but no other possible value. Similarly, Python’s str data type is designed for storing strings of text.Python’s built‐in data types mostly fall into six categories: numerics for numbers;...
Monday, August 22, 2022
Creating a Variable and Assigning Data to It
In Python, you create a variable and assign data to it in a single statement. For example, consider the following line:price = 125This line (A) declares a variable called price and initializes it by assigning the value 125 to it. This value is an integer, a number with no decimal component, so...
Friday, August 19, 2022
Understanding Variables and Their Usage
Python supports various different data types, such as integers for whole‐number values, Booleans for True/False values, and strings for words or other sequences of text characters. After creating a variable, you can assign any type of data to it that Python uses. So What Is a Variable?A variable...
Wednesday, August 10, 2022
Import a Module or Object Under an Alias
The standard way of importing a module or an object adds it to Python’s mapping table, but Python also enables you to import a module or object under an alias of your choice. Using an alias can make your code more compact and easier to read.Because you have not imported the module, you cannot refer to the object via the module. So if you have imported only the path object from the os module, you cannot...
Monday, August 8, 2022
Importing Modules and Objects
When you load Python using the python or python3 command, depending on the operating system, Python loads its core modules, which provide essential functionality. When you need further functionality, you can import one or more additional modules, files containing Python code.For example, when you need to work with directories, such as creating or deleting them, you can import the os module, which...
Saturday, August 6, 2022
Understanding Comments in Python
Python enables you to add comments to your code. A comment is text that appears in a script but that is marked as not to be executed. You can add comments to your code at any point. For example, as you develop a script, you might use comments to describe the tasks the code needs to perform and possible approaches for them. After finishing the script’s commands, you might rework the comments so that...
Wednesday, August 3, 2022
Understanding the main() Function
Many Python scripts include a function called main() that contains the main set of actions the script performs. In this post, you learn the purpose of the main() function and when and how to create one.You also learn about the two ways to run code using the Python interpreter. How you run a script affects how Python sets the built‐in __name__ parameter, which you can use to control whether the main()...