Login

Data Types in Programming: Numbers, Strings and Others

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: How to Organize Data Using Data Structures: Files, Arrays, Lists and Others

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:15 Strings
  • 1:44 Numeric Data Types
  • 3:09 Boolean Data
  • 3:53 Composite Data Types
  • 6:13 Date and Time
  • 6:53 Enumerated
Add to Add to Add to

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Login or Sign up

Timeline
Autoplay
Autoplay
Create an account to start this course today
Try it free for 5 days!
Create An Account

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Paul Zandbergen

Paul has a PhD from the University of British Columbia and has taught Geographic Information Systems, statistics and computer programming for 15 years.

Within a programing language, a data type of an object determines what type of values an object can have and what operations can be performed on the object. Learn about the most common data types, including strings, numbers, Booleans, lists and arrays.

Data Types

Programming uses a number of different data types. A data type determines what type of values an object can have and what operations can be performed.

Strings

One of the most widely used data types is a string. A string consists of one or more characters, which can include letters, numbers, or other types of characters. You can think of a string as plain text.

A string represents alphanumeric data. This means that a string can contain many different characters, but they are all considered as if they were text, even if the characters are numbers. A string can also contain spaces. This presents a bit of an issue. How are you going to distinguish between the value of a string and the actual code of the program? The solution is to mark the beginning and end of a string with a special character, typically a quote. For example, the following code is used to print text to the screen:

print 'Hello World!'

The use of quotes ensures that the text 'Hello World!' is recognized as a string and not as two separate words that may have some special meaning in the programming language. The use of quotes also makes it possible to use numeric characters as part of a string. For example, the following code is used to store a street address:

address = '123 Central Avenue'

Without the quotes, the numeric characters 123 would be interpreted as a number, but with the quotes, it is recognized as being part of a string that represents a street address.

Numeric Data Types

The second most important data type is numeric data. There are several different ones.

An integer is a numeric value without a decimal. Integers are whole numbers and can be positive or negative. Sometimes a distinction is made between short and long integers, referring to how much data storage is used for the number. A short integer is typically stored using 16 bits, which means you can store up to 2^16, or 65,536, unique values. For any numbers larger than that, you would need to use a long integer, which uses 32 bits or more.

A number with a decimal is referred to as a decimal, a float or a double. The terminology varies somewhat with the programming language being used. The term 'float' comes from floating point, which means you can control where the decimal point is located. The term 'double' refers to using double the amount of storage relative to a float. Working with numbers in code is a little bit like using a calculator. Here is an example of how numeric values are used in code, in this case using a multiplication character:

result = 3 * 117.89

The value stored in the 'result' would be 353.67.

Boolean Data

The Boolean data type can only represent two values: true or false. Typically, a 1 is used to represent True, and a 0 is used to represent False. Consider the following example where a user inputs two values and the program determines whether the first one is smaller than the second one or not.


pseudocode


In this example the first value is in fact not smaller than the second one, and the program therefore results in a Boolean value of False. The Boolean type is the primary results of conditional statements, which are used to control workflow in program. For example, if a particular condition is true, then do this - if the condition is false, then do something else.

Composite Data Types

The data types covered so far are often referred to as primitive data types. A composite data type is obtained by combining more than one primitive data type. These are also referred to as data structures. Common examples of composite data types are lists and arrays.

A list contains elements of one particular data type. For example, a list could contain strings. An example would be the names of all players on a soccer team. Each name is a string, but when you organize all the names together, they form a list. A list is the simplest data structure.

For example, a list of strings could look like this:

('John', 'Paul', 'George', 'Ringo')

A list of numbers could look like this:

(67, 84, 92, 52, 81, 75)

Each element in a list is identified by a specific index. All elements in a list are ordered in a particular sequence. This sequence does not change unless you actively change elements or sort the sequence. Each element has an index, which tells you at what position in the sequence that element is located. Typically the index value of the first position is zero (0). In the example above, the number 67 is located at index position 0, and the number 81 is located as index position 4.

An array is a data type where the elements are identified by one or more indices. An array is similar to a list, but an array can have multiple dimensions. A one-dimensional array is the same as a list: a linear sequence of elements that are all of the same type.

To unlock this lesson you must be a Study.com Member.
Create your account

Register for a free trial

Are you a student or a teacher?
I am a teacher
What is your educational goal?
 Back

Unlock Your Education

See for yourself why 10 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back

Earning College Credit

Did you know… We have over 79 college courses that prepare you to earn credit by exam that is accepted by over 2,000 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it free for 5 days!
Create An Account
Support