0% found this document useful (0 votes)
5 views

Datatypes in Python1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Datatypes in Python1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Data types are nothing but the different types of input data accepted by a

programming language for defining, declaring, storing and performing


mathematical & logical values/ operations.
var1 = 20
var2 = 20.65
var3 = "Hello!, World “
print( type(var1) )
print( type(var2) )
print( type(var3) );
1. Numbers

When a number is assigned to a variable Number class object is created.


2. String
The string can be defined as the sequence of characters represented in
the quotation marks.
In python, the string can be quoted by single, double, or triple quotes.

s = 'hello! how are you’


print (s[1])
print (s[2:6])
s1 = 'hello world’
print (s + s1)
3. Tuple
Tuples also store the collection of the elements of different data types.
A tuple is the same as the list, but a tuple is immutable (non-editable or
cannot modify the size and elements value).
To create a tuple uses the () simple parenthesis; within this brackets, store all
the elements separated with the comma (,).
4. List
List stores a collection of different types of elements. The list is
mutable (editable). It is the same as arrays in C, but the list stores
elements of different data types. To create a list uses the [] square
brackets; within these brackets, stores all the elements separated
with the comma (,). We can use index[i], slice [:] operators,
concatenation operator (+), repetition operator (*) etc., to works with
the list the same as with the strings.
5. Set
Set also stores the collection of the elements of different data
types. A Set is the same as the list and tuple, but the set is
immutable (non-editable or cannot modify the size and elements
value), un order and stores only the unique elements. To create a
set uses the {} curly brackets, within this brackets stores all the
elements separated with the comma (,).
st = {"apple", "banana", 100, 20.78}
# set cannot support indexing st[1]
# set cannot support slicing st[1:]
print (st)
print (st + st) # set cannot support concatenation
print (st * 2) # set cannot support repetition
print (type(st))
st[2] = "hi" # set is immutable
6. Dictionary
Dictionary is also stored in a collection of different data types elements in
the form of key-value pairs. It is ordered, mutable and stores unique keys
as a set. To create a set uses the {} curly brackets same as a set, within this,
brackets stores all the elements (key-value pair) separated with the comma
(,).
Expressions in Python

➢ An expression is a combination of operators and operands that is


interpreted to produce some other value.
➢ We have many different types of expressions in Python.

1. Constant Expressions: These are the expressions that have constant values only.
2. Arithmetic Expressions: An arithmetic expression is a combination of numeric
values, operators, and sometimes parenthesis.
3. Integral Expressions: These are the kind of expressions that produce only integer
results after all computations and type conversions.
4. Floating Expressions: These are the kind of expressions which produce floating
point numbers as result after all computations and type conversions.
5. Relational Expressions: In these types of expressions, arithmetic expressions
are written on both sides of relational operator (> , < , >= , <=).

These expressions are also called Boolean expressions.


6. Logical Expressions: These are kinds of expressions that result in
either True or False. It basically specifies one or more conditions.
8. Combinational Expressions: We can also use different types of expressions in a
single expression, and that will be termed as combinational expressions.
Strings
Strings in python are surrounded by either single quotation marks, or
double quotation marks.
'hello' is the same as "hello".
You can display a string literal with the print() function:
Assign String to a Variable
Assigning a string to a variable is done with the variable name followed
by an equal sign and the string:
Strings are Arrays
Like many other popular programming languages, strings in
Python are arrays of bytes representing Unicode characters.
However, Python does not have a character data type, a single
character is simply a string with a length of 1.
Square brackets can be used to access elements of the string.
Python - Modify Strings
Python Operators
Python Identity Operators

You might also like