Storing and Modifying Information

Storing and Modifying Information: CRUD (an acronym: Create, Read, Update, and Delete) what tasks all computer programs perform with the information you want to manage.

To make information useful, you have to have some means of storing it permanently. Otherwise, every time you turned the computer off, all your information would be gone and the computer would provide limited value. In addition, Python must provide some rules for modifying information.

An application requires fast access to information or else it will take a long time to complete tasks. As a result, applications store information in memory. However, memory is temporary. When you turn off the machine, the information must be stored in some permanent form, such as on your hard drive, a Universal Serial Bus (USB) flash drive, or a Secure Digital (SD) card. In addition, you must also consider the form of the information, such as whether it’s a number or text.

Looking at variables as storage boxes

When working with applications, you store information in variables. A variable is a kind of storage box. Whenever you want to work with the information, you access it using the variable. If you have new information you want to store, you put it in a variable. Changing information means accessing the variable first and then storing the new value in the variable. Just as you store things in boxes in the real world, so you store things in variables (a kind of storage box) when working with applications.

Each variable stores just one piece of information. Using this technique makes it easy to find the particular piece of information you need. Most applications rely heavily on variables to make working with information easier.

Using the right box to store the right data

People tend to store things in the wrong box. For example, you might find a pair of gloves in a garment bag and a supply of socks in a shoebox. However, Python likes to be organized. As a result, you find numbers stored in one sort of variable and text stored in an entirely different kind of variable.

You use variables in both cases, but the variable is designed to store a particular kind of information. Using specialized variables makes it possible to work with the information inside in particular ways. You don’t need to worry about the details just yet — just keep in mind that each kind of information is stored in a special kind of variable.

Python uses specialized variables to store information to make things easy for the programmer and to ensure that the information remains safe. However, computers don’t actually know about information types. All the computer knows about are 0s and 1s, which is the absence or presence of a voltage.

At a higher level, computers do work with numbers, but that’s the extent of what computers do. Numbers, letters, dates, times, and any other kind of information you can think about all come down to 0s and 1s in the computer system.

For example, the letter B is actually stored as 01000010 or the ASCII code number 065. The computer has no concept of the letter B or of a date such as 11/17/1980.

Defining the Essential Python Data Types

Python defines variables that hold specific kinds of information. The specific kind of variable is called a data type. Knowing the data type of a variable is important because it tells you what kind of information you find inside. In addition, when you want to store information in a variable, you need a variable of the correct data type to do it. Python doesn’t allow you to store text in a variable designed to hold numeric information. Doing so would damage the text and cause problems with the application. You can generally classify Python data types as numeric, string, and Boolean, although there really isn’t any limit on just how you can view them. The following sections describe each of the standard Python data types within these classifications.

Putting information into variables

To place a value into any variable, you make an assignment using the assignment operator (=). For example, to place the number 7 into a variable named myVar, you type myVar = 7 Even though Python doesn’t provide any additional information to you, you can always type the variable name and press Enter to see the value it contains.

Understanding the python-numeric types

Human beings tend to think about numbers in general terms. We view 2 and 2.0 as being the same number — one of them simply has a decimal point. However, as far as we’re concerned, the two numbers are equal and we could easily use them interchangeably. Python views them as being different kinds of numbers because each form requires a different kind of processing.


Any whole number is an integer. For example, the value 1 is a whole number, so it’s an integer. On the other hand, 1.0 isn’t a whole number; it has a decimal part to it, so it’s not an integer. Integers are represented by the int data type. As with storage boxes, variables have capacity limits. Trying to stuff a value that’s too large into a storage box results in an error. On most platforms, you can store numbers between –9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 within an int (which is the maximum value that fits in a 64-bit variable).

When working with the int type, you have access to a number of interesting features.

  • Base 2: Uses only 0 and 1 as numbers.
  • Base 8: Uses the numbers 0 through 7.
  • Base 10: Uses the usual numeric system.
  • Base 16: Is also called hex and uses the numbers 0 through 9 and the letters A through F to create 16 different possible values.
  • To tell Python when to use bases other than base 10, you add a 0 and a special letter to the number. For example, 0b100 is the value one-zero-zero in base 2. Here are the letters you normally use:

  • b: Base 2
  • o: Base 8
  • x: Base 16
  • It’s also possible to convert numeric values to other bases using the bin(), oct(), and hex() commands.

    Floating-point values

    Any number that includes a decimal portion is a floating-point value. For example, 1.0 has a decimal part, so it’s a floating-point value. Many people get confused about whole numbers and floating-point numbers, but the difference is easy to remember. If you see a decimal in the number, then it’s a floatingpoint value. Python stores floating-point values in the float data type. Floating-point values have an advantage over integer values in that you can store immensely large or incredibly small values in them. As with integer variables, floating-point variables have a storage capacity. In their case, the maximum value that a variable can contain is ±1.7976931348623157 × 10308 and the minimum value that a variable can contain is ±2.2250738585072014 × 10-308 on most platforms. When working with floating-point values, you can assign the information to the variable in a number of ways. The two most common methods are to provide the number directly and to use scientific notation. When using scientific notation, an e separates the number from its exponent. Note that using a negative exponent results in a fractional value.

    Complex numbers

    A complex number consists of a real number and an imaginary number that are paired together. Just in case you’ve completely forgotten about complex numbers, you can read about them at Real-world uses for complex numbers include:

  • Electrical engineering
  • Fluid dynamics
  • Quantum mechanics
  • Computer graphics
  • Dynamic systems
  • Complex Numbers in Real Life: University of Toronto Mathematics network

    Complex numbers have other uses, too, but this list should give you some ideas. In general, if you aren’t involved in any of these disciplines, you probably won’t ever encounter complex numbers. However, Python is one of the few languages that provides a built-in data type to support them. As you progress through the tutorials, you find other ways in which Python lends itself especially well to science and engineering.

    The imaginary part of a complex number always appears with a j after it. So, if you want to create a complex number with 7 as the real part and 3 as the imaginary part, you make an assignment like this:

    myComplex = 7 + 3j

    Understanding the need for multiple number types

    A lot of computer programmers have a hard time understanding why there is a need for more than one numeric type. After all, humans can use just one kind of number. To understand the need for multiple number types, you have to understand a little about how a computer works with numbers. An integer is stored in the computer as simply a series of bits that the computer reads directly. A value of 0100 in binary equates to a value of 4 in decimal. On the other hand, numbers that have decimal points are stored in an entirely different manner. Exponents — they actually come in handy sometimes. A floating-point number is stored as a sign bit (plus or minus), mantissa (the fractional part of the number), and exponent (the power of 2). (Some texts use the term significand in place of mantissa — the terms are interchangeable.) To obtain the floating-point value, you use the equation:

    Value = Mantissa * 2^Exponent

    At one time, computers all used different floating-point representations, but they all use the IEEE-754 standard now. You can read about this standard at A full explanation of precisely how floating-point numbers work can be found here. Nothing helps you understand a concept like playing with the values. You can find a really interesting floating-point number converter at, where you can click the individual bits (to turn them off or on) and see the floating-point number that results.

    As you might imagine, floating-point numbers tend to consume more space in memory because of their complexity. In addition, they use an entirely different area of the processor — one that works more slowly than the part used for integer math. Finally, integers are precise, as contrasted to floating-point numbers, which can’t precisely represent some numbers, so you get an approximation instead. However, floating-point variables can store much larger numbers. The bottom line is that decimals are unavoidable in the real world, so you need floating-point numbers, but using integers when you can reduce the amount of memory your application consumes and helps it work faster. There are many trade-offs in computer systems, and this one is unavoidable.

    Understanding Boolean values

    Computers always give you a straight answer! A computer will never provide “maybe” as output. Every answer you get is either True or False. In fact, there is an entire branch of mathematics called Boolean algebra that was originally defined by George Boole (a super-geek of his time) that computers rely upon to make decisions. Contrary to common belief, Boolean algebra has existed since 1854 — long before the time of computers.

    Determining a variable’s type

    Sometimes you might want to know the variable type. Perhaps the type isn’t obvious from the code or you’ve received the information from a source whose code isn’t accessible. Whenever you want to see the type of a variable, use the type() method. For example, if you start by placing a value of 5 in myInt by doing the following:

    myInt = 5

    We found the variable type of myInt by typing print(type(myInt)). The output will be < class'int' >, which means that myInt contains an int value.

    The window below is an online python compiler. Go ahead and run the sample code below and see the output for yourself: on-line Python compiler

    Understanding strings

    Of all the data types, strings are the most easily understood by humans and not understood at all by computers. If you have read the previous chapters in this book, you have already seen strings used quite. For example, all the example code in Chapter 4 relies on strings. A string is simply any grouping of characters you place within double quotes. For example, myString = “Python is a great language.” assigns a string of characters to myString.

    The computer doesn’t see letters at all. Every letter you use is represented by a number in memory. For example, the letter A is actually the number 065.

    To see this for yourself, type print(ord(“A”)) at the Python prompt and run the code. (Try it below in the on-line Python compiler.) on-line Python compiler

    You see 65 as output. Its possible to convert any single letter to its numeric equivalent using the ord() command. Because the computer doesn’t really understand strings, but strings are so useful in writing applications, you sometimes need to convert a string to a number. You can use the int() and float() commands to perform this conversion.

    For example, if you type myInt = int(“123”) and run code at the Python prompt compiler, you create an int named myInt that contains the value 123. You can convert numbers to a string as well by using the str() command. For example, if you type myStr = str(1234.56) and run code, you create a string containing the value “1234.56” and assign it to myStr. Bottom line, you can go back and forth between strings and numbers with ease.

    Working with Dates and Times

    Dates and times are items that most people work with quite a bit. Society bases almost everything on the date and time that a task needs to be or was completed. We make appointments and plan events for specific dates and times. Most of our day revolves around the man-made clock. Because of the time-oriented nature of humans, it’s a good idea to look at how Python deals with interacting with dates and time (especially storing these values for later use).

    As with everything else, computers understand only numbers — the date and time don’t really exist.

    To work with dates and times, you need to perform a special task in Python. To use dates and times, you must issue a special import datetime command. Technically, this act is called importing a module. Don’t worry how the command works right now — just use it whenever you want to do something with date and time.

    Computers do have clocks inside them, but the clocks are for the humans using the computer. Yes, some software also depends on the clock, but again, the emphasis is on human needs rather than anything the computer might require. To get the current time, you can simply type and run the code. You see the full date and time information as found on your computer’s clock. on-line Python compiler

    You may have noticed that the date and time are a little hard to read in the existing format. Say you want to get just the current date, in a readable format. It’s time to combine a few things you discovered in previous lessons to accomplish that task. Type print(str( and run code, shows that you now have something a little more usable. on-line Python compiler

    Python also has a time() command, which you can use to obtain the current time. You can obtain separate values for each of the components that make up date and time using the day, month, year, hour, minute, second, and microsecond values.

    Connected through code, Choose Your Platform!

    About the Author: Bernard Aybout

    In the land of bytes and bits, a father of three sits, With a heart for tech and coding kits, in IT he never quits. At Magna's door, he took his stance, in Canada's wide expanse, At Karmax Heavy Stamping - Cosma's dance, he gave his career a chance. With a passion deep for teaching code, to the young minds he showed, The path where digital seeds are sowed, in critical thinking mode. But alas, not all was bright and fair, at Magna's lair, oh despair, Harassment, intimidation, a chilling air, made the workplace hard to bear. Management's maze and morale's dip, made our hero's spirit flip, In a demoralizing grip, his well-being began to slip. So he bid adieu to Magna's scene, from the division not so serene, Yet in tech, his interest keen, continues to inspire and convene.