Skip to main content

Basics of Python - 1

 Data types in Python

Datatypes are the attributes or  group of specific characters in computer science which tells the compiler or the interpreter about the type of data in which the user want a specific character to be used. Whenever a specific datatype is mentioned by the user, the compiler or the interpreter converts the data to the specific type to be read by the computer. It plays a very vital role in the conversation between the computer and the programmer and without having sufficient types of data it would be really confusing to program any code at all. 

The built-in core datatypes provided in Python are:

  1. String
  2. Numbers
  3. List
  4. Tuple
  5. Dictionary

String

A string datatype literally holds the actual string data (which may be letters, numbers or special symbols) inside "" or inside '' ,i.e. inside double or single quotation marks.

For example: All the following strings are legal and acceptable in Python:
"Hello","Holla",'I am great.','My age is 16.',"!@#$%^&*"

For extra knowledge: All the string characters in python(3.x) are unicode characters.

Numbers

A number datatype as it sounds is used to store numeric values in python. Number datatypes can undergo mathematical operations like addition, subtraction, multiplication, division, etc. (whose article will be provided later in the blog)

Numbers are further classified in python into the following categories:
    1. Integers
  • Integers(numeric or normal integers)
  • Booleans(having only two values)
    2. Floating-point numbers 
         (or Floats)
    3. Complex numbers

Integers:

Integers as general mathematical term mean the set of whole numbers along with the negative ones. They don't have any fractional parts. For example: 23,-45,0,etc.

  • Integers: These are the normal integer representations and can be of any whole value, i.e. negative, positive or 0. In python(3.x), the integers can be of any length, only dependent on the memory available. For example: 23,-45,0,etc.
  • Booleans: These types of integers accept only two values, i.e. True(1) or False(2). To see the representation of the boolean values in python, you may have to type bool(1) or bool(0) which would give an output of True or False respectively.

Floats:

The numbers which have a fractional or decimal part can be represented using a float. All the floating point numbers always consist of a decimal point. For example: 2.346757, 12.376, 67.332, 0.0463, etc. Another interesting fact about the floats is that these numbers have a precision of 15 digits.
Note: In python, any number, say 17, represents an integer type of number, but the same value with a decimal point, i.e. 17.0 represents a float type number.

Complex Numbers:

A complex number is as it sounds a complex type of number consisting of two components, viz. real and imaginary components. It can be represented in the form A + iB where A stands for the imaginary part whereas B stands for the imaginary part and  represents the Greek letter iota.
Good for knowledge:  i = √-1

Lists
A list in python is generally a list of different values separated by commas enlisted with square brackets. The values may be string, numbers, lists,etc.
For example: [1,34,56,3]
                          ["hello","world","name"]
                          ["aeiou',1,2,3,[4,5],6]



Tuples
Tuple are those lists of values as elements which are immutable separated by commas and enlisted within (). The values may be string, numbers, tuples, etc.
For example: (1,34,65,87,98,543,56)
                         ("holla","amigo","good")
                         ("night","morning",23,45,43,(23,"hey","hi"))



Dictionary
Dictionary is that type of data in which there is a unordered list of keys and values
within {}.
For example: {"Name" : "Raj", "Age in years" : 16, "Fav. fruit" : "Mango"}



Thanks for reading!

©SGPython

Comments

Post a Comment

Popular posts from this blog

Our view towards Python (as a programming language)

  Python - The language of the future Introduction When we hear the term programming language, we usually talk about high-level programming language which are those programming language having a lucid (or easy-to-read) syntax and are later converted to low-level language (binary) , which is understandable by the computer.  Programming language has always been an area of interest over the passing years since the very early days of ' Fortran ' (created by John Backus at  IBM ). To this day, dozens of programming language has been created by different 'Computer Scientists' around the world. What is Python? What is Python? Many of you will describe a large scary snake 🐍, which isn't actually wrong 😅. But here we are talking about a programming language. Similarly, Python is also a high-level and interepreted programming language created by  Guido van Rossum in the late 1980s . It was first released in 1991 as a successor to ABC Programming language . It known fo...

For Loops

 The for loop By  polygraphus  on  shutterstock Def: The for loop is also known as the counting loop and is designed to process the items of any sequence, such as a list or a string, one by one. General form of for loop: The general form of the for loop is given below:      for <variable> in <sequence>:          statements to repeat

Type-Casting And Dynamic-Typing

Python is equipped with some of the amazing and easy to use methods which are already built-in keeping the ease of understanding and fun-to-learn. Some of these amazing properties are going to be discussed today in the blog. Type-Casting Typecasting is one of the simple topics of Python which which is very much easy to understand yet is very efficient in the day-to-day lives of Python programmers. Def:  The explicit conversion of an operand to a specific type is called  typecasting. Explicit conversion refers to the type of conversion of datatype into which the user wants to change his or her expression into. Note that these types of conversion are user defined. Typecasting in Python can be explained as follows:                         <datatype> (expression) where <datatype> refers to the datatype into which the user wants to type-cast. Example:  When we use input() function, Python alwa...