AFTER COMPLETION EXCERCISE: https://www.w3schools.com/python/exercise.asp?filename=exercise_datatypes1
Python - Data Types
Python Data Types are used to define the type of a variable. It defines what type of data we are going to store in a variable. The data stored in memory can be 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 the following data types built-in by default, in these categories:
Text Type:
str
Numeric Types:
int, float, complex
Sequence Types:
list, tuple, range
Data Types in Python
MyVar1 ="Some Text"# This is a text or Stringprint(MyVar1)
MyVar2 =12# This is an integerprint(MyVar2)
Different Data Types in Python
Data Type
To Change
Denoted by
String
str()
” “
Integer
int()
None
Float
float()
None
Lists
list()
[“a”,”b”]
Tuples
tuple()
(“a”,”b”)
Dictionary
dict(name = “somename”,work =”somework”)
{“a”:”b”,”c”:”d”}
Boolean
bool(1)
True / False
Sets
set((“a”,”b”))
{“a”,”b”,”c”}
Data Types in Python
#Different Data Types stored in some variables
somestr ="anyname"
someint =12
somefloat =15.25
sometuple =('abc','xyz','mno')
somedict ={"abc":"bcd","cde":"def"}
somebool = true
someset ={'abc','xyz','mno'}
Now let’s dig all the data types one by one.
Strings in Python
Strings Literals
To make a string in python you need to include the text between quotes
You can Use either single quotes or double quotes. Like of the example given below
Example: Datatypes
# Can use either single or double quotesprint("Using double quotes")print('Using single quotes')
Single Line – Strings in Variables
You can easily assign string values to variables
Create a variable and set its value to a string with using single or multiple quote as discussed earlier
Single Line Strings
# Can use either single or double quotes
dblQuoteVar ="Using double quotes"
singleQtVar ="Using single quotes"print(dblQuoteVar)print("and")print(singleQtVar)
Multi-Line – Strings in Variables
These were the single line strings stored in a variable(s).
Now we will How to store multiline strings as variables.
Multi-Line - Strings in Variables
multilineString ="""
This is a multiline string
This is using three double quotes
we can also three single quotes
\n
"""print(multilineString)# or
multiline_single ='''
This is a multiline string
This is using three double quotes
we can also three single quotes
'''print(multiline_single)
Note: “\n” character is used to give a line break, A new line starts after “\n” and hence its also known as New Line Character.
----------------------------------------------
Setting the Data Type
In Python, the data type is set when you assign a value to a variable:
Post a Comment