Wednesday, July 14, 2021

Python 101: Data Types

Data Types are kinds of data which tell the compiler or interpreter the kind of inputs a variable can accept.

Python Data Types
1) integers {int} : this is basically whole numbers.
2) floating point numbers {float} : this is basically any number that is not whole (contains decimal points)
3)complex numbers {complex}: they are written in the form of 'x + yj' where x is real and y is imaginary. Example 2+3j
4) List: this is similar to arrays in any other programming language. They are collections of data Types (must not be the same).
a = [1, 3.4, 1+2j, 'help']
Nb: a[2], a[3] etc is used to access individual contents of the list.
Here a[0]=1, a[1]=3.4, a[2]=1+2j and a[3]='help'
You can also use a[0:2] to print everything from [0] to [1] not [2]
And
a[1:] to print everything after [1]
You can also change the content of the list by simply referring to the particular position in the array and switching it like you would any other data 
5)Tuples: you can take this as the constant version of a list. They have everything similar to list except they are declared with parentheses and not square brackets. And unlike list, tuples cannot be altered once declared.
6) Strings: this is a sequence of characters. To represent strings we use single or double quotes while triple quotes are used for multi lined strings.

You can also access individual components of a string using square brackets but you can't change their values
7) Sets: this is just like the normal set you learnt in maths. It is a collection of similar items and is inside the curly braces {}.
Sets in python are programmed to automatically delete duplicated values.

You can also perform all sorts of set functions in python

8) Dictionary: consists of a collection of key-value pairs. A dictionary is a collection which is ordered*, changeable and does not allow duplicates.

If you wish to print an individual item from the dictionary, you use:




NB: type() is used to find out which class a variable belongs to while the isinstance() is used to check if an object belongs to a particular class.
The syntax:
type(a)
isinstance (a,float)

a can be replace with a value and float can be replaced with any data type.


No comments:

Post a Comment

HTML 101: Form

  I believe that everyone who has made use of the internet knows what a form is but in case you don't know, a form is a way of receiving...