Values and Data types in Python
Value
• A value is one of the fundamental things, like
a number, a character or a string that program
manipulates.
Example
2,42.0,’Hello,World’ The value belong to
different types.
Literal Constants
 The value of a literal constant can be used directly in programs. For example,
7, 3.9, 'A', and "Hello" are literal constants.
 Numbers refers to a numeric value. You can use four types of numbers in
Python program- integers, long integers, floating point and complex numbers.
 Numbers like 5 or other whole numbers are referred to as integers. Bigger
whole numbers are called long integers. For example, 535633629843L is a
long integer.
 Numbers like are 3.23 and 91.5E-2 are termed as floating point numbers.
 Numbers of a + bi form (like -3 + 7i) are complex numbers.
9/5/2022 Introduction to python 3
Example
9/5/2022 Introduction to python 4
Strings
A string is a group of characters.
• Using Single Quotes ('): For example, a string can be written as 'HELLO'.
• Using Double Quotes ("): Strings in doubles are exactly same as those in
single quotes. Therefore, 'HELLO' is same quoteas "HELLO".
• Using Triple Quotes (''' '''): You can specify multi-line strings using triple
quotes. You can use as many single quotes and double quotes as you
want in a string within triple quotes.
Examples:
• Data type refers to the type and size of data.
Variables can hold values of different data types.
Python is a purely object oriented language. It
refers to everything as an object, including
numbers and strings.
• The standard data types supported by python
includes:
– Number
– Boolean
– String
– List
– Tuple
– Dictionary
Numbers
• Numbers refers to a numeric value. It includes integers, long
integers, floating point, and complex numbers.
• Integers are whole numbers with no fractional parts. They can be
either positive, negative or zero value. Python displays long
integers with an uppercase L (56788333354533L is a long integer).
• Floating point numbers are numbers with fractions or decimal
points. Floating point values can be expressed in scientific notation
using the letter ‘e’ or ‘E’.
• A complex number is a number that can be expressed in the form a
+ bi, where a and b are real numbers, and i is the imaginary unit.
• The type() function can be used to know which data type
a variable or a value belongs to.
• The isinstance() function to check if an object belongs to
a particular class.
Example:
a = 5
print(a, "is of type", type(a))
a = 2.0
print(a, "is of type", type(a))
a = 1+2j
print(a, "is complex number?", isinstance(1+2j,complex))
Output:
5 is of type <class 'int'>
2.0 is of type <class 'float'>
(1+2j) is complex number? True
• Boolean is another data type in Python.
• A variable of Boolean type can have one of the
two values- True or False.
List
• List is an ordered sequence of items. It is one of
the most used data type in Python and is very
flexible. All the items in a list do not need to be of
the same type.
• Lists are mutable. The elements in the list can be
modified.
• To declare a list in python, separate the items
using commas and enclose them within square
brackets [ ].
>>> a = [1, 2.2, 'python'] The slicing operator [ ] is
used to extract an item or a range of items from a
list. Index starts form 0 in Python.
Example:
>>>a = [5,10,15,20,25,30,35,40]
>>>a[2]
15
>>>print("a[0:3] = ", a[0:3])
[5,10,15]
• Tuple
• Tuple is an ordered sequence of items same as
list. The only difference is that tuples are
immutable. Tuples once created cannot be
modified.
• It is defined within parentheses ( ) where items
are separated by commas.
Example:
>>> t = (5,'program', 1+3j)
print("t[1] = ", t[1])
print("t[0:3] = ", t[0:3])
Output:
t[1] = program
t[0:3] = (5, 'program', (1+3j))
Strings
• A String in python can be a series or a
sequence of alphabets, numerals and special
characters.
• Single quotes or double quotes are used to
represent strings.
• >>> s = "This is a string"
• Strings are immutable.
Example:
s = 'Hello world!'
print("s[4] = ", s[4]) # s[4] = 'o'
print("s[6:11] = ", s[6:11]) # s[6:11] = 'world'
Output:
s[4] = o
s[6:11] = world

2. Values and Data types in Python.pptx

  • 1.
    Values and Datatypes in Python
  • 2.
    Value • A valueis one of the fundamental things, like a number, a character or a string that program manipulates. Example 2,42.0,’Hello,World’ The value belong to different types.
  • 3.
    Literal Constants  Thevalue of a literal constant can be used directly in programs. For example, 7, 3.9, 'A', and "Hello" are literal constants.  Numbers refers to a numeric value. You can use four types of numbers in Python program- integers, long integers, floating point and complex numbers.  Numbers like 5 or other whole numbers are referred to as integers. Bigger whole numbers are called long integers. For example, 535633629843L is a long integer.  Numbers like are 3.23 and 91.5E-2 are termed as floating point numbers.  Numbers of a + bi form (like -3 + 7i) are complex numbers. 9/5/2022 Introduction to python 3
  • 4.
    Example 9/5/2022 Introduction topython 4 Strings A string is a group of characters. • Using Single Quotes ('): For example, a string can be written as 'HELLO'. • Using Double Quotes ("): Strings in doubles are exactly same as those in single quotes. Therefore, 'HELLO' is same quoteas "HELLO". • Using Triple Quotes (''' '''): You can specify multi-line strings using triple quotes. You can use as many single quotes and double quotes as you want in a string within triple quotes. Examples:
  • 5.
    • Data typerefers to the type and size of data. Variables can hold values of different data types. Python is a purely object oriented language. It refers to everything as an object, including numbers and strings. • The standard data types supported by python includes: – Number – Boolean – String – List – Tuple – Dictionary
  • 6.
    Numbers • Numbers refersto a numeric value. It includes integers, long integers, floating point, and complex numbers. • Integers are whole numbers with no fractional parts. They can be either positive, negative or zero value. Python displays long integers with an uppercase L (56788333354533L is a long integer). • Floating point numbers are numbers with fractions or decimal points. Floating point values can be expressed in scientific notation using the letter ‘e’ or ‘E’. • A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers, and i is the imaginary unit.
  • 7.
    • The type()function can be used to know which data type a variable or a value belongs to. • The isinstance() function to check if an object belongs to a particular class. Example: a = 5 print(a, "is of type", type(a)) a = 2.0 print(a, "is of type", type(a)) a = 1+2j print(a, "is complex number?", isinstance(1+2j,complex)) Output: 5 is of type <class 'int'> 2.0 is of type <class 'float'> (1+2j) is complex number? True
  • 8.
    • Boolean isanother data type in Python. • A variable of Boolean type can have one of the two values- True or False. List • List is an ordered sequence of items. It is one of the most used data type in Python and is very flexible. All the items in a list do not need to be of the same type. • Lists are mutable. The elements in the list can be modified. • To declare a list in python, separate the items using commas and enclose them within square brackets [ ].
  • 9.
    >>> a =[1, 2.2, 'python'] The slicing operator [ ] is used to extract an item or a range of items from a list. Index starts form 0 in Python. Example: >>>a = [5,10,15,20,25,30,35,40] >>>a[2] 15 >>>print("a[0:3] = ", a[0:3]) [5,10,15]
  • 10.
    • Tuple • Tupleis an ordered sequence of items same as list. The only difference is that tuples are immutable. Tuples once created cannot be modified. • It is defined within parentheses ( ) where items are separated by commas.
  • 11.
    Example: >>> t =(5,'program', 1+3j) print("t[1] = ", t[1]) print("t[0:3] = ", t[0:3]) Output: t[1] = program t[0:3] = (5, 'program', (1+3j))
  • 12.
    Strings • A Stringin python can be a series or a sequence of alphabets, numerals and special characters. • Single quotes or double quotes are used to represent strings. • >>> s = "This is a string" • Strings are immutable.
  • 13.
    Example: s = 'Helloworld!' print("s[4] = ", s[4]) # s[4] = 'o' print("s[6:11] = ", s[6:11]) # s[6:11] = 'world' Output: s[4] = o s[6:11] = world