Making communications land - Are they received and understood as intended? we...
Basic data types in python
1. BASIC DATA TYPES IN PYTHON
PROF. SUNIL D. CHUTE
HEAD DEPT OF COMPUTER SCIENC
M.G. COLLEGE ARMORI
2. BASIC DATA TYPES IN PYTHON
Data types are the classification or
categorization of data items. It represents the
kind of value that tells what operations can
be performed on a particular data. Since
everything is an object in Python
programming, data types are actually classes
and variables are instance (object) of these
classes.
3. One way to categorize these basic data types is in
one of four groups:
Numeric: int, float and the less frequently
encountered complex
Sequence: str (string), list and tuple
Boolean: (True or False)
Dictionary: dict(dictionary) data type, consisting
of (key, value) pairs
NOTE: It's important to point out that Python usually doesn't require you
to specify what data type you are using and will assign a data type to
your variable based on what it thinks you meant.
An equally important thing to point out is that Python is a
"loosely/weakly typed" programming language, meaning that a variable
can change its type over the course of the program's execution, which
isn't the case with "strongly typed" programming languages (such as
Java or C++).
4.
5. NUMERIC DATA TYPES
These data types are fairly straight-forward and represent
numeric values. These can be decimal values, floating point
values or even complex numbers.
Integer Data Type - int
The int data type deals with integers values. This means
values like 0, 1, -2 and -15, and not numbers like 0.5, 1.01, -
10.8, etc.
If you give Python the following code, it will conclude that a is
an integer and will assign the int data type to it:
>>> x = 5
>>> type(x)
<class 'int'>
We could have been more specific and said something along these
lines, to make sure Python understood our 5 as an integer, though,
it'll automatically do this exact same thing under the hood:
6. >>> x = int(5)
>>> type(x)
<class 'int‘>
It's worth noting that Python treats any sequence of numbers (without a
prefix) as a decimal number. This sequence, in fact, isn't constrained.
That is to say, unlike in some other languages like Java, the value of
the int doesn't have a maximum value - it's unbounded.
The sys.maxsize may sound counterintuitive then, since it implies that that's
the maximum value of an integer, though, it isn't.
>>> x = sys.maxsize
>>> x
2147483647
This appears to be a 32-bit signed binary integer value, though, let's see
what happens if we assign a higher number to x:
>>> x = sys.maxsize
>>> x+1
2147483648
7. In fact, we can even go as far as:
>>> y = sys.maxsize + sys.maxsize
>>> y
4294967294
The only real limit to how big an integer can be is the memory
of the machine you're running Python on.
Prefixing Integers
What happens when you'd like to pack a numeric value in a
different form? You can prefix a sequence of numbers and tell
Python to treat them in a different system.
More specifically, the prefixes:
0b or 0B - Will turn your integer into Binary
0o or 0O - Will turn your integer into Octal
0x or 0X - Will turn your integer into Hexadecimal
8. # Decimal value of 5
>>> x = 5
>>> x
5
# Binary value of 1
>>> x = 0b001
>>> x
1
# Octal value of 5
>>> x = 0o5
>>> x
5
# Hexadecimal value of 10
>>> x = 0x10
>>> x
16
9. FLOATING POINT DATA TYPE - FLOAT
The float type in Python designates a floating-point number. float values
are specified with a decimal point. Optionally, the
character e or E followed by a positive or negative integer may be
appended to specify scientific notation
Ex.1 >>> 4.2 4.2
>>> type(4.2)
<class 'float'>
Ex.2 >>> 4.
4.0
Ex.3 >>> .2
0.2
Ex.4>>> .4e7 4000000.0
>>> type(.4e7)
<class 'float'>
Ex.5>>> 4.2e-4
10. COMPLEX NUMBERS
Complex numbers are specified as <real
part>+<imaginary part>j.
Ex. >>> 2+3j
(2+3j)
>>> type(2+3j)
<class 'complex'>
11. STRINGS
Strings are sequences of character data. The string
type in Python is called str.
String literals may be delimited using either single or
double quotes. All the characters between the opening
delimiter and matching closing delimiter are part of the
string:
Ex.>>> print("I am a string.")
I am a string.
>>> type("I am a string.")
<class 'str'>
Ex.>>> print('I am too.')
I am too.
>>> type('I am too.')
<class 'str'>
12. A string in Python can contain as many characters as you
wish. The only limit is your machine’s memory resources.
A string can also be empty:
Ex. >>> ''
''
What if you want to include a quote character as part of
the string itself? Your first impulse might be to try
something like this:
Ex.>>> print('This string contains a single quote (')
character.')
SyntaxError: invalid syntax
Ex. >>> mystring = "This is not my first String"
>>> print (mystring);
This is not my first String
13. to join two or more strings.
Ex.>>> print ("Hello" + "World");
HelloWorld
Ex. >>> s1 = "Name Python "
>>> s2 = "had been adapted "
>>> s3 = "from Monty Python"
>>> print (s1 + s2 + s3)
Name Python had been adapted from Monty
Python
14. use input() function
Ex.>>> n = input("Number of times you
want the text to repeat: ")
Number of times you want the text to
repeat:
>>> print ("Text"*n);
TextTextTextTextText
Check existence of a character or a sub-
string in a string
Ex.>>> "won" in "India won the match"
True
15. PYTHON LIST DATA TYPE
List is an ordered sequence of items. It is one of
the most used datatype in Python and is very
flexible. All the items in a list do not need to be
of the same type.
Declaring a list is pretty straight forward. Items
separated by commas are enclosed within
brackets [ ].
Ex. a = [1, 2.2, 'python']
We can use the slicing operator [ ] to extract an
item or a range of items from a list. The index
starts from 0 in Python.
16.
17. PYTHON TUPLE DATA TYPE
Tuple is an ordered sequence of items same
as a list. The only difference is that tuples are
immutable. Tuples once created cannot be
modified.
Tuples are used to write-protect data and are
usually faster than lists as they cannot
change dynamically.
It is defined within parentheses () where
items are separated by commas.
Ex. t = (5,'program', 1+3j)
18. We can use the slicing operator [] to extract
items but we cannot change its value.
19. PYTHON SET DATA TYPE
Set is an unordered collection of unique
items. Set is defined by values separated by
comma inside braces { }. Items in a set are
not ordered.
A set is created by placing all the items
(elements) inside curly braces {}, separated
by comma, or by using the built-
in set() function.
It can have any number of items and they
may be of different types (integer, float, tuple,
string etc.). But a set cannot have mutable
20.
21.
22. PYTHON DICTIONARY DATA TYPE
Python dictionary is an unordered collection
of items. Each item of a dictionary has
a key/value pair.
It is generally used when we have a huge
amount of data. Dictionaries are optimized
for retrieving data. We must know the key to
retrieve the value.
In Python, dictionaries are defined within
braces {} with each item being a pair in the
form key:value. Key and value can be of any
type.