Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Python:Variables and Data Types

python-variables
python-data-types

Object References

Object Identity

Variable Names(Multiple Assignment)

Python Data Types(type)

Numbers(int,complex no,float)
Dictionary(key-value pair)
(i) d[1].
(ii) d[4].
(iii) d.keys()
(iv) d.values()
boolean
set
(i)set2.add(10)
(ii)set2.remove(2)
sequence type
(i)string--str1[0:2], str1[4], str1*2,str1 + str2
(ii)list--list1,list1[3:], list1[0:2],list1 + list1,list1 * 3
(iii) tuple--tup[1:], tup[0:1], tup + tup, tup * 3

Object References

(print("John")  )
Output:
John

type("John") 
Output:
<class 'str'>
Enter fullscreen mode Exit fullscreen mode

Object Identity

a = 50  
print(id(a)) 
Output:
140734982691168
Enter fullscreen mode Exit fullscreen mode

Variable Names
*Multiple Assignment *

x=y=z=50   

print(x)    
print(y)    
print(z)    
Output:

50  
50  
50  
a,b,c=5,10,15
print a    
print b    
print c    
Output:

5  
10  
15  
Enter fullscreen mode Exit fullscreen mode

Python Data Types

a=10  
b="Hi Python"  
c = 10.5  
print(type(a))  
print(type(b))  
print(type(c))  
Output:

<type 'int'>
<type 'str'>
<type 'float'>
Enter fullscreen mode Exit fullscreen mode

Numbers

print(" c is a complex number", isinstance(1+3j,complex))nnn 
Enter fullscreen mode Exit fullscreen mode

Sequence Type

str1 = 'hello javatpoint' #string str1    
str2 = ' how are you' #string str2    
print (str1[0:2]) #printing first two character using slice operator    
print (str1[4]) #printing 4th character of the string    
print (str1*2) #printing the string twice    
print (str1 + str2) #printing the concatenation of str1 and str2 
Enter fullscreen mode Exit fullscreen mode

Output:

he
o
hello javatpointhello javatpoint
hello javatpoint how are you
Enter fullscreen mode Exit fullscreen mode

List

list1  = [1, "hi", "Python", 2]    
print (list1)    
# List slicing  
print (list1[3:])    
# List slicing  
print (list1[0:2]) 

# List Concatenation using + operator  
print (list1 + list1) 

# List repetation using * operator  
print (list1 * 3)  
Enter fullscreen mode Exit fullscreen mode
Output:

[1, 'hi', 'Python', 2]
[2]
[1, 'hi']
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
Enter fullscreen mode Exit fullscreen mode

Tuple

tup  = ("hi", "Python", 2)    
print (tup[1:])    
print (tup[0:1])   
print (tup + tup)    
print (tup * 3) 
Enter fullscreen mode Exit fullscreen mode

Output:

('Python', 2)
('hi',)
('hi', 'Python', 2, 'hi', 'Python', 2)
('hi', 'Python', 2, 'hi', 'Python', 2, 'hi', 'Python', 2)
Enter fullscreen mode Exit fullscreen mode

Dictionary

d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}   
print("1st name is "+d[1])   
print("2nd name is "+ d[4])     
print (d.keys())    
print (d.values()) 
Enter fullscreen mode Exit fullscreen mode

Output

{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}
1st name is Jimmy
2nd name is mike
dict_keys([1, 2, 3, 4])
dict_values(['Jimmy', 'Alex', 'john', 'mike'])
Enter fullscreen mode Exit fullscreen mode

Set

set2 = {3, 'Python', 'James', 2}  
print(set2)
set2.add(10)  
print(set2)   
set2.remove(2)  
print(set2) 
Enter fullscreen mode Exit fullscreen mode

Output:

{3, 'Python', 'James', 2}
{'Python', 'James', 3, 2, 10}
{'Python', 'James', 3, 10}
Enter fullscreen mode Exit fullscreen mode

Python Variables

Variable is a name that is used to refer to memory location. Python variable is also known as an identifier and used to hold value.

In Python, we don't need to specify the type of variable because Python is a infer language and smart enough to get variable type.

Variable names can be a group of both the letters and digits, but they have to begin with a letter or an underscore.

It is recommended to use lowercase letters for the variable name. Rahul and rahul both are two different variables.
Object Identity
In Python, every created object identifies uniquely in Python. Python provides the guaranteed that no two objects will have the same identifier. The built-in id() function, is used to identify the object identifier. Consider the following example.

a = 50  
b = a  
print(id(a))  
print(id(b))  
# Reassigned variable a  
a = 500  
print(id(a)) 
Enter fullscreen mode Exit fullscreen mode
Output:

140734982691168
140734982691168
2822056960944
Enter fullscreen mode Exit fullscreen mode

Variable Names
We have already discussed how to declare the valid variable. Variable names can be any length can have uppercase, lowercase (A to Z, a to z), the digit (0-9), and underscore character(_). Consider the following example of valid variables names.

name = "Devansh"  
age = 20  
marks = 80.50  

print(name)  
print(age)  
print(marks) 
Enter fullscreen mode Exit fullscreen mode
Output:

Devansh
20
80.5
Enter fullscreen mode Exit fullscreen mode

Assigning multiple values to multiple variables:

Eg:

a,b,c=5,10,15    
print a    
print b    
print c 
Enter fullscreen mode Exit fullscreen mode
Output:

5  
10  
15  
Enter fullscreen mode Exit fullscreen mode

Python Data Types

Variables can hold values, and every value has a data-type. Python is a dynamically typed language; hence we do not need to define the type of the variable while declaring it. The interpreter implicitly binds the value with its type.

a = 5  


a=10  
b="Hi Python"  
c = 10.5  
print(type(a))  
print(type(b))  
print(type(c)) 
Enter fullscreen mode Exit fullscreen mode

Output:

<type 'int'>
<type 'str'>
<type 'float'>
Enter fullscreen mode Exit fullscreen mode

Image description

Numbers
Number stores numeric values. The integer, float, and complex values belong to a Python Numbers data-type. Python provides the type() function to know the data-type of the variable. Similarly, the isinstance() function is used to check an object belongs to a particular class.

Python creates Number objects when a number is assigned to a variable. For example;

a = 5  
print("The type of a", type(a))  

b = 40.5  
print("The type of b", type(b))  

c = 1+3j  
print("The type of c", type(c))  
print(" c is a complex number", isinstance(1+3j,complex)) 
Enter fullscreen mode Exit fullscreen mode

Output:

The type of a <class 'int'>
The type of b <class 'float'>
The type of c <class 'complex'>
c is complex number: True
Enter fullscreen mode Exit fullscreen mode

Sequence Type
String
The string can be defined as the sequence of characters represented in the quotation marks. In Python, we can use single, double, or triple quotes to define a string.

String handling in Python is a straightforward task since Python provides built-in functions and operators to perform operations in the string.

In the case of string handling, the operator + is used to concatenate two strings as the operation "hello"+" python" returns "hello python".

The operator * is known as a repetition operator as the operation "Python" *2 returns 'Python Python'.

Consider the following example of string handling.

Example - 2

str1 = 'hello javatpoint' #string str1    
str2 = ' how are you' #string str2    
print (str1[0:2]) #printing first two character using slice operator    
print (str1[4]) #printing 4th character of the string    
print (str1*2) #printing the string twice    
print (str1 + str2) #printing the concatenation of str1 and str2  
Enter fullscreen mode Exit fullscreen mode
Output:

he
o
hello javatpointhello javatpoint
hello javatpoint how are you
Enter fullscreen mode Exit fullscreen mode

List
Python Lists are similar to arrays in C. However, the list can contain data of different types. The items stored in the list are separated with a comma (,) and enclosed within square brackets [].

We can use slice [:] operators to access the data of the list. The concatenation operator (+) and repetition operator (*) works with the list in the same way as they were working with the strings.

Consider the following example.

list1  = [1, "hi", "Python", 2]    
#Checking type of given list  
print(type(list1))  

#Printing the list1  
print (list1)  

# List slicing  
print (list1[3:])  

# List slicing  
print (list1[0:2])   

# List Concatenation using + operator  
print (list1 + list1)  

# List repetation using * operator  
print (list1 * 3) 
Enter fullscreen mode Exit fullscreen mode

Output:

[1, 'hi', 'Python', 2]
[2]
[1, 'hi']
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]

Enter fullscreen mode Exit fullscreen mode

Tuple
A tuple is similar to the list in many ways. Like lists, tuples also contain the collection of the items of different data types. The items of the tuple are separated with a comma (,) and enclosed in parentheses ().

A tuple is a read-only data structure as we can't modify the size and value of the items of a tuple.

Let's see a simple example of the tuple.

tup  = ("hi", "Python", 2)    
# Checking type of tup  
print (type(tup))    

#Printing the tuple  
print (tup)  

# Tuple slicing  
print (tup[1:])    
print (tup[0:1])    

# Tuple concatenation using + operator  
print (tup + tup)    

# Tuple repatation using * operator  
print (tup * 3)     

# Adding value to tup. It will throw an error.  
t[2] = "hi"  
Enter fullscreen mode Exit fullscreen mode

Output:

<class 'tuple'>
('hi', 'Python', 2)
('Python', 2)
('hi',)
('hi', 'Python', 2, 'hi', 'Python', 2)
('hi', 'Python', 2, 'hi', 'Python', 2, 'hi', 'Python', 2)

Traceback (most recent call last):
  File "main.py", line 14, in <module>
    t[2] = "hi";
TypeError: 'tuple' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode

Dictionary
Dictionary is an unordered set of a key-value pair of items. It is like an associative array or a hash table where each key stores a specific value. Key can hold any primitive data type, whereas value is an arbitrary Python object.

The items in the dictionary are separated with the comma (,) and enclosed in the curly braces {}.

Consider the following example.

d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}  
Enter fullscreen mode Exit fullscreen mode

Printing dictionary
print (d)

Accesing value using keys

print("1st name is "+d[1])   
print("2nd name is "+ d[4])    

print (d.keys())    
print (d.values())
Enter fullscreen mode Exit fullscreen mode

Output:

1st name is Jimmy
2nd name is mike
{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}
dict_keys([1, 2, 3, 4])
dict_values(['Jimmy', 'Alex', 'john', 'mike'])
Enter fullscreen mode Exit fullscreen mode

**

Set
Python Set is the unordered collection of the data type. It is iterable, mutable(can modify after creation), and has unique elements. In set, the order of the elements is undefined; it may return the changed sequence of the element. The set is created by using a built-in function set(), or a sequence of elements is passed in the curly braces and separated by the comma. It can contain various types of values. Consider the following example.

Creating Empty set

set1 = set()  

set2 = {'James', 2, 3,'Python'}  

#Printing Set value  
print(set2)  

# Adding element to the set  

set2.add(10)  
print(set2)  

#Removing element from the set  
set2.remove(2)  
print(set2) 
Enter fullscreen mode Exit fullscreen mode

Output:

{3, 'Python', 'James', 2}
{'Python', 'James', 3, 2, 10}
{'Python', 'James', 3, 10}
Enter fullscreen mode Exit fullscreen mode

Summary

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description
More Example on variable and string

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Top comments (0)