0% found this document useful (0 votes)
14 views6 pages

DataTypes in Python

The document provides an overview of data types in Python, including numeric, string, boolean, and collection types, along with their properties and examples. It explains Python's dynamic typing and how to check variable types using the type() function. Additionally, it covers user input handling, type casting, and the use of the print() function for output, including formatting options and escape characters.

Uploaded by

imchepan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

DataTypes in Python

The document provides an overview of data types in Python, including numeric, string, boolean, and collection types, along with their properties and examples. It explains Python's dynamic typing and how to check variable types using the type() function. Additionally, it covers user input handling, type casting, and the use of the print() function for output, including formatting options and escape characters.

Uploaded by

imchepan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data Types in Python Programming

A data type defines the kind of value a variable can store, and the operations that can be
performed on it. For instance, Python has several built-in data types, including numeric types
(int, float, complex), string (str), boolean (bool), and collection types (list, tuple, dict, set).
Moreover, each data type has its own set of properties, methods, and behaviors that allow
programmers to manipulate and process data effectively in their programs.

Python is dynamically typed, meaning the data type is assigned at runtime, not explicitly by the
programmer. So,

VariableName = Value (either a constant value or a user input or the result of a formula or
return value of a function)

Whatever the type of data a ‘Value’ has, the ‘VariableName’ will hold the same type of data.
For example, if ‘Value’ is 123 i.e. a ‘int’ data type then ‘VariableName’ will also has ‘int’
datatype.

• Numeric Data Types: Numeric data types in Python are used to represent numerical

values. Python provides three primary numeric datatype in python:

 Integer (int): Integers are whole numbers without any decimal points. They can

be positive or negative.

Example: x = 10, y = -5

 Floating-Point (float): Floating-point numbers represent decimal values. They can

be positive or negative and may contain a decimal point.

Example: pi = 3.14, price = 99.0

 Complex (complex): People use complex numbers to represent numbers with a

real and imaginary part. You write them in the form of a + bj, where a is the real

part and b is the imaginary part.

Example: z = 3 + 5j
• String Data Type(str): Represents a sequence of characters enclosed in single quotes

(‘StringValue ‘) or double quotes (”StringValue“) or triple quotes (‘’’StringValue‘’’ or

“””StringValue“””, such as “Hello, World. This is Python-Programming”, ‘Graphic Era Hill

University’, ‘’’This is the year 2026’’’, “””2026 is not a leap year”””, etc.

• Boolean Data Type(bool): Represents either True or False, used for logical operations

and conditions.

• Collection Data Types:

 list: Represents an ordered and mutable collection of items, enclosed in square

brackets [value1, value2, ......].

 tuple: Represents an ordered and immutable collection of items, enclosed in

parentheses (value1, value2, .....).

 dict: Represents a collection of key-value pairs enclosed in curly braces

{Key1:Value1, Key2:Value2, ........} with unique keys.

 set: Represents an unordered and mutable collection of unique elements,

enclosed in curly braces {value1, value2, .....} or using the set() function.

In order to check the data type of a variable, you can use the built-in type() function to see the
data type of any variable:

type(Value/VariableName)

NOTE that this function returns a value which represents the class of data type, for example,

x = 10.5

print(type(x))

Output: <class 'float'>


Asking the user for input/ Printing the values or messages
In order to take user input, we have a built-in function “input()”. Its syntax is:

VariableName = input("message for the user: ")

OR

VariableName = input()

NOTE that the input() function always returns a string (str) value, even if the user types a
number, it always store in the variable in the form of a string data type only.

To use input for mathematical calculations (int or float), you must convert it to an int or float.
This is called Type Casting.

for storing input value as integer:

VariableName = int(input("message for the user: "))

VaribleName = int(input())

For storing input value as float:

VariableName = float(input("message for the user: "))

VaribleName = float(input())

For example,

age = int(input("Enter your age: "))

print("Next year your age will be", age + 1)

price = float(input("Enter the item price: "))

print("Plateform fee is 1.1, so the new price with tax is", price * 1.1)

To print the values, we have print() function, one of the most frequently used function in
Python. Its primary job is to send data to the "standard output" (usually your screen) so you can
see the results of your code. To display text, you must wrap the text in quotes (either single ' or
double " or triple ''' or “””). For example,
print('this is python programming')

print("python has very simple and easy to understand programming syntax")

print("""python is widely used for data science and artificial intelligence programming""")

Output:

this is python programming


python has very simple and easy to understand programming syntax
python is widely used for data science and artificial intelligence
programming

NOTE: Triple quotes are also used to print a multi-line output, for
example

print("""python has following properties:

1. it is a object oriented programming

2. it is a scripting language

3. it is a high level programming language

""")

Output:

python has following properties:


1. it is a object oriented programming
2. it is a scripting language
3. it is a high level programming language

You can print multiple variables or values at once by separating them with a comma. Python
will automatically add a space between them. For example,

print("name and age of this person is as follows:")

# print("type your message", VariableName1, "type your message", VariableName2, ...............)

name, age = "Alice", 25

print("""User Name:""", name, """, Age:""", age)

Output:
name and age of this person is as follows:
User Name: Alice , Age: 25
The print() function has two hidden parameters that allow you to
control how the output looks. They are: ‘sep’ which has by default
value ‘ ‘ i.e. space, and ‘end’ which has by default value “\n” i..e
newline. Thats why “print("""User Name:""", name, """, Age:""", age)” here every sub-
values are seperated by a space and a print statement always run in the next line. However we
can control these values by replacing default value by the required value. For example,

print("good morning student", "how are you all", "all is well")

print("good morning student", "how are you all", "all is well", sep='@')

print("")

print("good morning students")

print("how are you all")

print("all is well")

print("good morning students", end='. ')

print("how are you all", end=". ")

print("all is well")

Output:

good morning student how are you all all is well


good morning student@how are you all@all is well

good morning students


how are you all
all is well
good morning students. how are you all. all is well

However, in order to put extra spaces or a new line within the quotes
(single or double or triple) of a print statement, we have an option
of using “Escape Characters”:

• \n: to print the new line


• \t: to put extra space i.e. the space created when you press Tab key (moves the cursor
to the next "tab stop". “Tab stops” are typically set every 4 or 8 characters.)
• \": to print double quote, if your whole string is already inside double quotes
• \': to print single quote
• \""": to print triple quote
print("good monring \nstudents") # use of \n
print("good\tmorning students") # use of \t
print("good morning \"students\"") # use of \""
print('good morning \'students\'') # use of \'
print("""good morning \"""students\""" """) # use of \""""""
Output:
good monring
students
good morning students
good morning "students"
good morning 'students'
good morning """students"""

NOTE: however in python inplace of \" and \' and \""" we may write the
print statement in the following way:
print("""good morning 'students'""") # here no need of Escape
Character
print('good morning "students"') # here no need of Escape Character

Output:

good morning 'students'


good morning "students"

You might also like