DEV Community

Cover image for Introduction to modern Python
Joseph Ngahu
Joseph Ngahu

Posted on

Introduction to modern Python

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.

Enter fullscreen mode Exit fullscreen mode


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.
Enter fullscreen mode Exit fullscreen mode


basket={“orange”,”banana”,”mango”}

Enter fullscreen mode Exit fullscreen mode


print(basket) #outputs the element in basket variable

##Frozen Sets##
They are immutable meaning no modification can be done to it.
Enter fullscreen mode Exit fullscreen mode


cities=frozenset({“Nairobi”,”Nakuru”,”Kisumu”,”Mombasa”})

Enter fullscreen mode Exit fullscreen mode


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

Enter fullscreen mode Exit fullscreen mode

-```

longNumber=1234L #long

-```

floatNumber=1.236 #float

Enter fullscreen mode Exit fullscreen mode

-```

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.
Enter fullscreen mode Exit fullscreen mode


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 ()
Enter fullscreen mode Exit fullscreen mode


tuple1=(“hello”)

Enter fullscreen mode Exit fullscreen mode


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[].
Enter fullscreen mode Exit fullscreen mode


dic={'name':'red','age':10}

Enter fullscreen mode Exit fullscreen mode


print(dic) #will output all the key-value pairs. {'name':'red','age':10}

Enter fullscreen mode Exit fullscreen mode


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.
Enter fullscreen mode Exit fullscreen mode


class ExampleClass:

Enter fullscreen mode Exit fullscreen mode

Every function belonging to a class must be indented equally

Enter fullscreen mode Exit fullscreen mode


def init(self):

Enter fullscreen mode Exit fullscreen mode


name = "example"

Enter fullscreen mode Exit fullscreen mode


def someFunction(self, a):

Enter fullscreen mode Exit fullscreen mode


#Notice everything belonging to a function must be indented

Enter fullscreen mode Exit fullscreen mode


if a > 5:

Enter fullscreen mode Exit fullscreen mode


return True

Enter fullscreen mode Exit fullscreen mode


else:

Enter fullscreen mode Exit fullscreen mode


return False

Enter fullscreen mode Exit fullscreen mode

If a function is not indented to the same level it will not be considers as part of the parent class

Enter fullscreen mode Exit fullscreen mode


def separateFunction(b):

Enter fullscreen mode Exit fullscreen mode


for i in b:

Enter fullscreen mode Exit fullscreen mode

Loops are also indented and nested conditions start a new indentation

Enter fullscreen mode Exit fullscreen mode


if i == 1:

Enter fullscreen mode Exit fullscreen mode


return True

Enter fullscreen mode Exit fullscreen mode


else:

Enter fullscreen mode Exit fullscreen mode


return False

Enter fullscreen mode Exit fullscreen mode


separateFunction([2,3,5,6,1])

Functions
Function is a module consisting of definition and statement
To declare a function we use ```

def

``` keyword
Enter fullscreen mode Exit fullscreen mode


def say_hello():

Enter fullscreen mode Exit fullscreen mode


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.
Enter fullscreen mode Exit fullscreen mode


marks= 100

Enter fullscreen mode Exit fullscreen mode


print(type(marks)) #returns int


print(str(marks)) #returns ‘100’




Enter fullscreen mode Exit fullscreen mode

Top comments (0)