Lesson 4 - Data types, Variables and Keywords
Computer Programming - Data Types
Let's discuss about a very simple but very important concept available in
almost all the programming languages which is called data types. As its
name indicates, a data type represents a type of the data which you can
process using your computer program. It can be numeric, alphanumeric,
decimal, etc.
Let’s keep Computer Programming aside for a while and take an easy
example of adding two whole numbers 10 & 20, which can be done
simply as follows −
10 + 20
Let's take another problem where we want to add two decimal numbers
10.50 & 20.50, which will be written as follows −
10.50 + 20.50
The two examples are straightforward. Now let's take another example
where we want to record student information in a notebook. Here we
would like to record the following information −
Name:
Class:
Section:
Age:
Sex:
Now, let's put one student record as per the given requirement −
Name: Zara Ali
Class: 6th
Section: J
Age: 13
Sex: F
The first example dealt with whole numbers, the second example added
two decimal numbers, whereas the third example is dealing with a mix of
different data. Let's put it as follows −
1
Student name "Zara Ali" is a sequence of characters which is also
called a string.
Student class "6th" has been represented by a mix of whole number
and a string of two characters. Such a mix is called alphanumeric.
Student section has been represented by a single character which
is 'J'.
Student age has been represented by a whole number which is 13.
Student sex has been represented by a single character which is
'F'.
This way, we realized that in our day-to-day life, we deal with different
types of data such as strings, characters, whole numbers (integers), and
decimal numbers (floating point numbers).
Similarly, when we write a computer program to process different types
of data, we need to specify its type clearly; otherwise the computer does
not understand how different operations can be performed on that given
data. Different programming languages use different keywords to specify
different data types. For example, C and Java programming languages
use int to specify integer data, whereas char specifies a character data
type.
Subsequent chapters will show you how to use different data types in
different situations. For now, let's check the important data types
available in C, Java, and Python and the keywords we will use to specify
those data types.
C and Java Data Types
C and Java support almost the same set of data types, though Java
supports additional data types. For now, we are taking a few common
data types supported by both the programming languages −
Type Keywo Value range which can be
rd represented by this data type
Character char -128 to 127 or 0 to 255
Number int -32,768 to 32,767 or -2,147,483,648 to
2,147,483,647
Small short -32,768 to 32,767
2
Number
Long long -2,147,483,648 to 2,147,483,647
Number
Decimal float 1.2E-38 to 3.4E+38 till 6 decimal
Number places
These data types are called primitive data types and you can use these
data types to build more complex data types, which are called user-
defined data type, for example a string will be a sequence of characters.
Python Data Types
Python has five standard data types but this programming language does
not make use of any keyword to specify a particular data type, rather
Python is intelligent enough to understand a given data type
automatically.
Numbers
String
List
Tuple
Dictionary
Here, Number specifies all types of numbers including decimal numbers
and string represents a sequence of characters with a length of 1 or more
characters. For now, let's proceed with these two data types and skip
List, Tuple, and Dictionary, which are advanced data types in Python.
Computer Programming - Variables
Variables are the names you give to computer memory locations which
are used to store values in a computer program.
For example, assume you want to store two values 10 and 20 in your
program and at a later stage, you want to use these two values. Let's see
how you will do it. Here are the following three simple steps −
Create variables with appropriate names.
Store your values in those two variables.
Retrieve and use the stored values from the variables.
3
Creating variables
Creating variables is also called declaring variables in C programming.
Different programming languages have different ways of creating
variables inside a program. For example, C programming has the
following simple way of creating variables −
#include <stdio.h>
int main() {
int a;
int b;
The above program creates two variables to reserve two memory
locations with names a and b. We created these variables
using int keyword to specify variable data type which means we want to
store integer values in these two variables. Similarly, you can create
variables to store long, float, char or any other data type. For example −
/* variable to store long value */
long a;
/* variable to store float value */
float b;
You can create variables of similar type by putting them in a single line
but separated by comma as follows −
#include <stdio.h>
int main() {
int a, b;
Listed below are the key points about variables that you need to keep in
mind −
A variable name can hold a single type of value. For example, if
variable a has been defined int type, then it can store only integer.
4
C programming language requires a variable creation, i.e.,
declaration before its usage in your program. You cannot use a
variable name in your program without creating it, though
programming language like Python allows you to use a variable
name without creating it.
You can use a variable name only once inside your program. For
example, if a variable a has been defined to store an integer value,
then you cannot define a again to store any other type of value.
There are programming languages like Python, PHP, Perl, etc.,
which do not want you to specify data type at the time of creating
variables. So you can store integer, float, or long without specifying
their data type.
You can give any name to a variable
like age, sex, salary, year1990 or anything else you like to give,
but most of the programming languages allow to use only limited
characters in their variables names. For now, we will suggest to
use only a....z, A....Z, 0....9 in your variable names and start their
names using alphabets only instead of digits.
Almost none of the programming languages allow to start their
variable names with a digit, so 1990year will not be a valid
variable name whereas year1990 or ye1990ar are valid variable
names.
Every programming language provides more rules related to variables
and you will learn them when you will go in further detail of that
programming language.
The multi-word keywords can be created by the following method.
o Camel Case - In the camel case, each word or abbreviation in the
middle of begins with a capital letter. There is no intervention of
whitespace. For example - nameOfStudent, valueOfVaraible, etc.
o Pascal Case - It is the same as the Camel Case, but here the first
word is also capital. For example - NameOfStudent, etc.
o Snake Case - In the snake case, Words are separated by the
underscore. For example - name_of_student, etc.
Store Values in Variables
You have seen how we created variables in the previous section. Now,
let's store some values in those variables −
5
#include <stdio.h>
int main() {
int a;
int b;
a = 10;
b = 20;
The above program has two additional statements where we are storing
10 in variable a and 20 is being stored in variable b. Almost all the
programming languages have similar way of storing values in variable
where we keep variable name in the left hand side of an equal sign = and
whatever value we want to store in the variable, we keep that value in
the right hand side.
Now, we have completed two steps, first we created two variables and
then we stored required values in those variables. Now variable a has
value 10 and variable b has value 20. In other words we can say, when
above program is executed, the memory location named a will hold 10
and memory location b will hold 20.
Access stored values in variables
If we do not use the stored values in the variables, then there is no point
in creating variables and storing values in them. We know that the above
program has two variables a and b and they store the values 10 and 20,
respectively. So let's try to print the values stored in these two variables.
Following is a C program, which prints the values stored in its variables −
#include <stdio.h>
int main() {
int a;
int b;
a = 10;
b = 20;
printf( "Value of a = %d\n", a );
6
printf( "Value of b = %d\n", b );
When the above program is executed, it produces the following result −
Value of a = 10
Value of b = 20
You must have seen printf() function in the previous chapter where we
had used it to print "Hello, World!". This time, we are using it to print the
values of variables. We are making use of %d, which will be replaced
with the values of the given variable in printf() statements. We can print
both the values using a single printf() statement as follows −
#include <stdio.h>
int main() {
int a;
int b;
a = 10;
b = 20;
printf( "Value of a = %d and value of b = %d\n", a, b );
When the above program is executed, it produces the following result −
Value of a = 10 and value of b = 20
If you want to use float variable in C programming, then you will have to
use %f instead of %d, and if you want to print a character value, then you
will have to use %c. Similarly, different data types can be printed using
different % and characters.
Variables in Java
Following is the equivalent program written in Java programming
language. This program will create two variables a and b and very
similar to C programming, it will assign 10 and 20 in these variables and
finally print the values of the two variables in two ways −
public class DemoJava {
7
public static void main(String []args) {
int a;
int b;
a = 10;
b = 20;
System.out.println("Value of a = " + a);
System.out.println("Value of b = " + b);
System.out.println("Value of a = " + a + " and value of b = " +
b);
When the above program is executed, it produces the following result −
Value of a = 10
Value of b = 20
Value of a = 10 and value of b = 20
Variables in Python
Following is the equivalent program written in Python. This program will
create two variables a and b and at the same time, assign 10 and 20 in
those variables.
Python does not want you to specify the data type at the time of variable
creation and there is no need to create variables in advance.
a = 10
b = 20
print "Value of a = ", a
print "Value of b = ", b
print "Value of a = ", a, " and value of b = ", b
8
When the above program is executed, it produces the following result −
Value of a = 10
Value of b = 20
Value of a = 10 and value of b = 20
You can use the following syntax in C and Java programming to declare
variables and assign values at the same time −
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
printf( "Value of a = %d and value of b = %d\n", a, b );
When the above program is executed, it produces the following result −
Value of a = 10 and value of b = 20
Computer Programming - Keywords
So far, we have covered two important concepts called variables and
their data types. We discussed how to use int, long, and float to specify
different data types. We also learnt how to name the variables to store
different values.
Though this chapter is not required separately because reserved
keywords are a part of basic programming syntax, we kept it separate to
explain it right after data types and variables to make it easy to
understand.
Like int, long, and float, there are many other keywords supported by C
programming language which we will use for different purpose. Different
programming languages provide different set of reserved keywords, but
there is one important & common rule in all the programming languages
that we cannot use a reserved keyword to name our variables, which
means we cannot name our variable like int or float rather these
keywords can only be used to specify a variable data type.
For example, if you will try to use any reserved keyword for the purpose
of variable name, then you will get a syntax error.
9
#include <stdio.h>
int main() {
int float;
float = 10;
printf( "Value of float = %d\n", float);
When you compile the above program, it produces the following error −
main.c: In function 'main':
main.c:5:8: error: two or more data types in declaration specifiers
int float;
......
Let's now give a proper name to our integer variable, then the above
program should compile and execute successfully −
#include <stdio.h>
int main() {
int count;
count = 10;
printf( "Value of count = %d\n", count);
C Programming Reserved Keywords
Here is a table having almost all the keywords supported by C
Programming language −
auto else long switch
break enum register typedef
case extern return union
char float short unsigned
10
const for signed void
continue goto sizeof volatile
default if static while
do int struct _Packed
double
int float;
Java Programming Reserved Keywords
Here is a table having almost all the keywords supported by Java
Programming language −
abstract assert boolean break
byte case catch char
class const continue default
do double else enum
extends final finally float
for goto if implements
import instanceof int interface
long native new package
private protected public return
short static strictfp super
switch synchronized this throw
11
throws transient try void
volatile while
Python Programming Reserved Keywords
Here is a table having almost all the keywords supported by Python
Programming language −
and exec not
assert finally or
break for pass
class from print
continue global raise
def if return
del import try
elif in while
else is with
except lambda yield
We know you cannot memorize all these keywords, but we have listed
them down for your reference purpose and to explain the concept
of reserved keywords. So just be careful while giving a name to your
variable, you should not use any reserved keyword for that programming
language.
12