Introduction to modern Python
Published: 2022-02-14 11:06:05

Python datatypes
Data type is a variable used to assign memory space in python. Though in python it is not necessarily to manually assign memory space for variables this happens automatically when a value is assigned to a variable.
##String datatype ##
String is a sequence of characters which are presented in quotation marks either single quotation marks or double quotation marks. Each time we make a change to the string variable a new string object is created.
``` name=”joseph”
```type(name) #outputs ‘str’ ```
##Set datatype##
Sets are unordered list of objects.
They are of two types:
-Sets
-Frozen sets
##Sets## 
Are immutable meaning new elements can be added or removed.
```basket={“orange”,”banana”,”mango”}```
```print(basket) #outputs the element in basket variable```
##Frozen Sets##
They are immutable meaning no modification can be done to it.
```cities=frozenset({“Nairobi”,”Nakuru”,”Kisumu”,”Mombasa”})```
```print(cities) #it outputs the elements in the  cities variable```
##Number datatype##
They are four types in python: int, long, float, complex.
-```marks=10 #integer```
-```longNumber=1234L #long```
-```floatNumber=1.236 #float```
-```ComplexNumber=12j #complex```
##List Datatype##
List datatype is similar to array whereby they elements are enclosed in square brackets []; list can store elements of different types unlike array which stores element of the same type.
``` list1=[1,2,”abn”,1.3,”asdf”]”```
##Tuple Datatype##
Tuples are similar to lists the difference is tuples cannot be changed and elements are enclosed in circular bracket ()
```tuple1=(“hello”)```
```tuple2=(1,2,3, “Word”)```
##Dictionary datatype##
Dictionary consists of key-value pairs. It is enclosed by curly braces {} and values can be assigned and accessed using square brackets[].
```dic={'name':'red','age':10}```
``` print(dic) #will output all the key-value pairs. {'name':'red','age':10}```
``` print(dic['name']) #will output only value with 'name' key. 'red'```
##Indentation##
Indentation is used in python instead of semi-colons. Indentation is  can be achieved by using tab key or four spaces.
```class ExampleClass: ```
 ```#Every function belonging to a class must be indented equally ```
```def __init__(self): ```
 ```name = "example" ```
```def someFunction(self, a): ```
``` #Notice everything belonging to a function must be indented ```
```if a > 5: ```
```return True ```
```else: ```
```return False ```
```#If a function is not indented to the same level it will not be considers as part of the parent class```
``` def separateFunction(b): ```
```for i in b: ```
```#Loops are also indented and nested conditions start a new indentation```
``` if i == 1: ```
```return True```
```else: ```
```return False ```
```separateFunction([2,3,5,6,1])```
Functions
Function is a module consisting of definition and statement
To declare a function we use ```def``` keyword
```def say_hello():```
``` print("Hello!")```
##Literals ##
A literal is a succinct and easily visible way to write a value. Literals represent the possible choices in primitive types for that language. Some of the choices of types of literals are often integers, floating point, Booleans and character strings. Python support the following literals:
-	String literals   ::   "halo" , '12345'
-	Int literals   ::   0,1,2,-1,-2
-	Long literals   ::   89675L
-	Float literals   ::   3.14
-	Complex literals   ::   12j
-	Boolean literals   ::   True or False
-	Special literals   ::   None
-	Unicode literals   ::   u"hello"
-	List literals   ::   [], [5,6,7]
-	Tuple literals   ::   (), (9,),(8,9,0)
-	Dict literals   ::   {}, {'x':1}
-	Set literals   ::   {8,9,10}
##Type conversion##
Is the process of converting a value of a particular data type to another datatype.
```marks= 100```
```print(type(marks)) #returns int
```print(str(marks)) #returns ‘100’