You are on page 1of 18

Chapter 3:

Variable and Data types

SU©2020
Introduction
  variable is name given to memory area to store value.
 It means that when you create a variable, you name space
in the memory.
 You can assigning different data types to the variables
 you can store integers or characters in these variables.

SU©2020
Assigning Values to Variables

 Python variables take data-type automatically when you


assign a value to a variable.
 The equal sign (=) is used to assign values to variables.
 a=11
 B= "Ahmed"

SU©2020
Multiple Assignment

 Python allows you to assign a single value to several


variables simultaneously.
 For example
 a= b = c = 1
 Its like c=1
b=c
a=b
 integer variables are created and all the three variables are
assigned to the same value.

SU©2020
Multiple Assignment…

 You can also assign multiple values to multiple


variables.
 Example
 a, b, c = 1, 2, "john "
 Here, two integer values 1 and 2 are assigned to the
variables a and b respectively and one string the
value "john" is assigned to the variable c.

SU©2020
Standard Data Types
 The data stored in variables can be one of many types.
 For example, a person's age is stored as a numeric value
and his or her address is stored as alphanumeric characters.
 Python has various standard data types.

SU©2020
Standard Data Types…

 Python has three standard data types-


 Numbers

 String
 List

SU©2020
Python Numbers

 Number data types variables store numeric values.


 Number variables are created when you assign a value to
them.
 For example
 var1= 1
 var2 = 10

SU©2020
Python Numbers…

 Python supports three different numerical types:


 int (integers)
• a=10
 float (floating point real values)
• b=5.4
 complex (complex numbers)
• C=3.14j

SU©2020
Python Numbers…
 Examples: Here are some examples of numbers

SU©2020
Python Numbers...
 A complex number consists of an ordered pair of real
numbers and the imaginary unit.
• 3.14j

SU©2020
Python Strings

 Strings in Python are identified as a contiguous set of


characters represented in the quotation marks.
 Python allows either pair of single or double quotes.

 For example:
 Str="welcome to Python“
 b = 'Hello World!'

SU©2020
Python Lists
 List is variable that can hold items of the same data-type or
different data-type.
 A list contains items separated by commas and enclosed
within square brackets ([]).
 To some extent, lists are similar to arrays in C.
 One of the differences between them is that all the items
belonging to a list can be of different data type.

SU©2020
Python Lists
 The values stored in a list can be accessed using the slice
operator ([ ] and [:]) with indexes starting at 0 in the
beginning of the list.
 The plus (+) sign is the list concatenation operator, and the
asterisk (*) is the repetition operator.

SU©2020
Python Lists
 For example:

• list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]


• tinylist = [123, 'john']
• print (list) # Prints complete list
• print (list[0]) # Prints first element of the list

• print (list[1:3]) # Prints elements starting from 2nd till 3rd


• print (list[2:]) # Prints elements starting from 3rd element
SU©2020
Some Question
 What are Variables?
 List five standard data types?
 What is Strings in Python?

 What is the list contains?

 What is the list?

SU©2020
Some Question…
 Python supports three different numerical data types list
them?
 Declare (create) integer data type variable

 Declare (create) float data type variable

 Declare (create) string data type variable


 What used plus (+) sign?

SU©2020
Have nice day

SU©2020

You might also like