Data types

Data types

Data types: 

Python supports several built-in data types such as integers, floating-point numbers, strings, lists, tuples, and dictionaries. The data type of a variable is determined by the value it holds. Here are some of the most common data types in Python:

  • Integer: Used to represent whole numbers, positive or negative. For example:
				
					x = 5   
				
			
  • Float: Used to represent decimal numbers. For example:
				
					y = 3.14
				
			
  • String: Used to represent text. A string is a sequence of characters enclosed in quotes, either single (‘ ‘) or double (” “). For example:
				
					z = "hello"
				
			
  • Boolean: Used to represent true/false values. The two possible values are True and False. For example:
				
					a = True
				
			
  • List: Used to store a collection of values, which can be of different data types. A list is created by enclosing values in square brackets, separated by commas. For example:
				
					b = [1, "hello", 3.14, True]
				
			
  • Tuple: Similar to a list, but immutable, which means that once created, its values cannot be changed. A tuple is created by enclosing values in parentheses, separated by commas. For example:
				
					c = (1, "hello", 3.14, True)
				
			
  • Dictionary: Used to store key-value pairs, where each key maps to a corresponding value. A dictionary is created by enclosing key-value pairs in curly braces, separated by commas. For example:
				
					d = {"name": "John", "age": 30, "gender": "male"}
				
			
  • Set: Used to store a collection of unique values, in no particular order. A set is created by enclosing values in curly braces, separated by commas. For example:
				
					e = {1, 2, 3, 4}
				
			

It’s important to know the data types in Python because different operations and functions are available for each type. You can use the type() function to determine the data type of a variable. For example: print(type(x)) will output <class ‘int’> if x is an integer.

Join To Get Our Newsletter
Spread the love