Hello readers! This is the third article of our tutorial series on Python - 'Python in Terminal' and we will be discussing about Constants, Variables and Basic data types in Python, in this article. In the previous two articles, we have had a brief introduction to Python and learned about Python installation on Linux systems. These two articles were very general, but from now on, we would be discussing Python Programming. Lets start our discussion with the definition of a Program.
A program is a sequence of instructions, which process on input data and produces the desired output. Constant, variables and data types are the basic building blocks of any computer program. So, it is very essential to know about these terms and this is from where we start programming.
Constants are fixed values that do not change during the execution of a program. Their properties are very much same as those in Mathematics. Python supports two types of constants that are listed below:
1. Numeric constants : These are numbers, may it be positive number, negative number, complex number, floating point number or an exponential number. Thus, in Python, following numbers are valid numeric constants:
4 + 3i, etc.
2. String constants : These are the sequence of alphanumeric characters, special characters and underscores enclosed in a single or double quotes. This makes following valid string constants :
"Your Own Linux!",
A variable is a named location in memory that stores a value. In simpler words, when we create a variable and assign a value to it, the operating system reserves a memory location and stores the value in it. This memory location can then be identified or accessed with a variable name. Values can be numbers, text, images, audio clips or any other data. Unlike constants which remain unchanged during execution of a program, variables may take different values at different times during the execution of a program. Upon declaration, each variable belongs to a
typethat determines what type of value is stored in it, like numerals, literals or strings, etc. Whenever we choose a variable name, it should be meaningful for better readability of a code. For example, variable names such as
team_sizemake more sense than variables
When we are to choose a variable name to store a value, we cannot simply select and use any alphanumeric string as a variable name, it must follow certain criteria. Following things must be taken care of while selecting a variable name:
- A variable name consist of alphabets, digits and underscores, and it must start with a letter or underscore
- No whitespaces are allowed
- It should not be a keyword
Note : Keywords are the reserved words in any programming language. They are predefined in libraries, have special meaning and purpose. Hence keywords are not allowed to be used as a variable names.
Below are the keywords used in Python:
and, assert, break, class, continue, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, yield
Considering above mentioned rules for a variable name, below are some valid and invalid variables names-
- Valid variable names :
- Invalid variable names :
We can assign a value to variable using the assignment operator
=, using the syntax
variable_name = value
>>> myValue = 5 >>> myValue 5
Built-in data types in Python
A data type is the type of value a Python object has. Depending upon this, we have different built-in data types in Python, which are -
complex. There are some more built-in data types such as
tuple, but they can be related to data organization in Python, so we can refer to them as 'Python data structures'. We will definitely study them in later part of the tutorial series and for now, we concentrate on very basic data types and know each of them one-by-one. Meanwhile, we can check what
typethe value belongs to using Python built-in
bool: A variable belonging to
booltype can store only two possible values
booltype variable can be used only in logical operations.
>>> myBool = True >>> type(myBool) <type 'bool'> >>> myBool = False >>> type(myBool) <type 'bool'>
int: A variable belonging to
inttype stores only Integer values.
>>> myInt = 5 >>> type(myInt) <type 'int'>
float: A variable belonging to
floattype stores floating point value.
>>> myFloat = 10.50 >>> type(myFloat) <type 'float'>
long: A variable belonging to
longtype can store big big values. To declare value as a
long, we have to append
lto the numeric value.
>>> myLong = 10l >>> type(myLong) <type 'long'>
complex: Complex numbers represents a real part and an imaginary part in the form
ais real part and
jbis imaginary part. Python provides built-in support for complex numbers.
>>> myComplex = 4 + 5j >>> type(myComplex) <type 'complex'> >>> myComplex (4+5j)
Depending upon the
typeof the values, we have different types of operations those we can perform on them. So, knowing about Python built-in data types is of utmost importance.
That's it for the scope of this article. We learned about Constants, Variables and different data types in Python, in this article. We are yet to be introduced to some other data types, like strings, lists, dictionaries and tuples, which we would be covering in a separate article, as they are more inclined towards data organization. In the next article, we will learn about basic arithmetic and logical operations in Python. Please let us know about your views and opinions in the comment section below and stay tuned. Thank you.