Monday, May 1, 2023

#PYTHON(DAY5) Data Types

 

#PYTHON(DAY5)

Data Types in python

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 various built-in data types which we will discuss with in this tutorial:

  • Numeric — int, float, complex
  • String — str
  • Sequence — list, tuple, range
  • Binary — bytes, bytearray, memoryview
  • Mapping — dict
  • Boolean — bool
  • Set — set, frozenset
  • None — NoneType
var1 = 1
var2 = 10
var3 = 20.5

Python supports four different numerical types −

  • int (signed integers)
  • long (long integers, they can also be represented in octal and hexadecimal)
  • float (floating point real values)
  • complex (complex numbers)

Examples

Here are some examples of numbers −

int : 10,20,-445,0x486

long : 51924361L,-0x19323L,0xDEFABCECBDAECBFBAEl,-052318172735L

float : 0.0,15.20,32.3+e18,-90.

complex : 3.14j,45.j,-.6545+0J

  • Python allows you to use a lowercase l with long, but it is recommended that you use only an uppercase L to avoid confusion with the number 1. Python displays long integers with an uppercase L.
  • A complex number consists of an ordered pair of real floating-point numbers denoted by x + yj, where x and y are the real numbers and j is the imaginary unit.

Example

Following is an example to show the usage of Integer, Float and Complex numbers:

# integer variable.
a=100
print("The type of variable having value", a, " is ", type(a))
# float variable.
b=20.345 print("The type of variable having value", b, " is ", type(b))
# complex variable.
c=10+3j print("The type of variable having value", c, " is ", type(c))

Python String Data Type

Python Strings are identified as a contiguous set of characters represented in the quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.

The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator in Python. For example −

str = 'Hello World!'

print (str) # Prints complete string
print (str[0]) # Prints first character of the string
print (str[2:5]) # Prints characters starting from 3rd to 5th
print (str[2:]) # Prints string starting from 3rd character
print (str * 2) # Prints string two times
print (str + "TEST") # Prints concatenated string
# output of above code
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST

In this story you will be finding only the numeric and string data types.

No comments:

Post a Comment

Building Static Website(part6) HTML Lists

  Building Static Website (part6) HTML Lists Today, let us add some lists to our detailed view section by using html lists. Lists: List is a...