Multiple Assignment and Data types
Multiple Assignment: Python programming
language allow, you to declaration of multiple variable and its assign multiple
value to the variables.
For Example:
X=y=z=10
Here, an integer object is created with the value 10, and all three
variables are assigned to the same memory location. You can also assign
multiple objects to multiple variables.
x,y,z = 25,35,”Ramakant”
Here, two integer objects with values 1 and 2 are assigned to variables x
and y respectively, and one string object with the value "Ramakant"
is assigned to the variable z.
For Example: Write a program demonstrates
the use of variable.
a = 55 #here a is variable of
integer type
b = 35
c = a + b
print("Addition of two Number is=",c)
welcome = "Welcome"
#here welcome is string variable
aryan = " Aryan Computers, Barshi"
welcome = welcome + " to " + aryan
print(welcome)
Output:
This is how you cast one variable type into another. Most of the other
variable types you typically wouldn't want to cast to. These are following a few of the important
variable types in Python:
- int(variable) - casts variable to integer
- str(variable) - casts variable to string
- float(variable) - casts variable to float
(number with decimal)
Basic Data Types:
The Data types
is used to store data. When user need to store the information inside computer
that time data types play important role.
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 various standard data types that are used
to define the operations possible on them and the storage method for each of
them.
In Python
programming, we not need to declare any data type while declaring a variable
like C or C++. We can simply just assign values to the variable.
These
are the following different data types used in python-
Number:
This data types
is used to store numeric values, including positive and negative number. Number objects are created when you assign a
value to them. Integers, floating point numbers and complex numbers to come
under python numbers category.
Any number you enter in Python
will be interpreted as a number, you are not required to declare what kind of
data type you are entering. Python programming will consider any number written
without decimals as an integer (as in 459) and any number
written with decimals as a float (as in 5.20).
Integers:
This data types is used to input whole integer number. Like in math, integers in computer
programming are whole numbers, which can be positive or negative. An integer
can also be known as an int. we cannot use commas within numbers.
Floating-Point Numbers:
A floating-point number or a float is a real
number, meaning that it can be either a positive or negative number. Because of
this, floating-point numbers can be numbers that can contain a fractional part,
such as 5.30 or -10.44.
Complex Number:
Complex numbers have a real and imaginary part, which are each implemented
using double in python programming.
For Example: Write a program to demonstrate use of various data
types.
a = 90
print(a, " is of type", type(a))
a = 2.90
print(a, "is of type", type(a))
a = 9+2j
print(a, "It is complex number",
isinstance(9+2j,complex))
Output:
String: Strings
are identified as a contiguous set of characters represented in the quotation marks.
Python programming allows for either pairs of single or double quotes. Strings
can be created by enclosing characters inside a single quote or double quotes.
Even triple quotes can be used in Python but generally used to represent
multiline strings. The plus (+) sign is the string concatenation operator and
the asterisk (*) is the repetition operator.
For Example: Write a program to demonstrate the
use of sting data type.
#
This is program for stings, all of the following are equivalent
s1 = 'BCA-I'
print(s1)
s2 = "BCA-II"
print(s2)
s3 = '''BCA-III''
print(s3)
# triple quotes string can extend multiple lines
s3 =
"""Hello, welcome to
Aryan Computers,
Barshi"""
print(s3)
Output:
#
Write a program to demonstrate the use of repatriation string.
s1 = ("Aryan" ' ') * 7
#Here S1 is a variable assigned value print seven time
print("Repeat the string
:",s1)
Output:
List: Lists are used
to store multiple items in a single variable name. Python offers a range of
compound data types often referred to as sequences. List is one of the most
frequently used and very versatile data type used in Python.
For Example:
Write a program to demonstrate the use of list
list1 = ["c", "c++", "Core
Java", "C#"] #Here list1
is name of the list
print("List is:",list1)
Output:
Tuple:
Tuples are used to store multiple items in a single variable. A tuple is a
collection which is ordered and unchangeable. In Python programming, a tuple is
similar to a list. The difference between the two is that we cannot change
the elements of a tuple once it is assigned whereas in a list, elements can be
changed.
Set: Sets are used to store multiple items in a single variable. A set
is a collection which is unordered, unchangeable*, and unindexed. A
set is an unordered collection of items. Every element is unique (no
duplicates) and must be immutable (which cannot be changed). However, the set
itself is mutable. We can add or remove items from it. Sets can be used to
perform mathematical set operations like union, intersection, symmetric
difference etc.
Dictionary:
Python dictionary is an unordered collection of items. While other compound
data types have only value as an element, a dictionary has a key: value pair.
Dictionaries are optimized to retrieve values when the key is known.
Dictionaries are used to store data values
in key:value pairs.Dictionaries are written with curly brackets, and have keys
and values.
For Example:
Write a program to demonstrate the use of list
d1 =
{"1": "Sunday", "2": "Monday",
"3": "Tuesday"}
print(d1)
Output:
Boolean:
Booleans are either true or false. This is used to assign Boolean values
directly. Expressions can also evaluate to a Boolean value. In certain places
(like if statements),
Python expects an expression to evaluate to a Boolean value. These places are
called Boolean contexts. You can use virtually any expression in a Boolean
context, and Python will try to determine its truth value. Different data types
have different rules about which values are true or false in a Boolean context.
Many
operations in math give us answers that evaluate to either true or false:
- Greater than
- 500 > 100 =True
- 1 > 5 =False
- Less than
- 200 < 400 =True
- 4 < 2 =False
- Equal
- 5 = 5 =True
- 500 = 400 =False
For
Example: Write a program to demonstrate the use
of Boolean data type.
a=5>10
print(a)
Output:
Comments
Post a Comment