Python complete tutorial handbook

Python tutorial for dummies

python: Beginner to Advanced

Python Variables

A variable means holding a value and these values will be stored in a reserved location.

To declare a variable, “=” is used to assign the value to a variable.

Type casting in Python

Casting is nothing but converting of one type to another.

In the below image, x is assigned as 5 and type x shows as int.

After casting to “str” , the type x shows as “str”

Python Data Types

A Data Type tells about the characteristic of a variable.

Python has six standard Data Types

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

Number

Number datatype has different types such as Integer, float etc.

As you can see below, this number belongs to float.

String & String Indexing

We can grab a letter in the string character using number index as demonstrated below. This will be useful when we see the examples below to pick up a letter in a string using python.

Character: TRAVEL

Index: 012345

Reverse Index: 0 -5 -4 -3 -2 -1

Note: 'str' object is not callable. you cannot use ().

As you can see below, I have assigned a variable named app for the string. If I need to pick the letter “t”, then I will use index 0 within the square brackets.

You can also use negative indexing like -1 to pick up the letter “u”

Now , in the below first case, if you need to pick the “diaries4u”, then you have to start from 6 and “:” means “all the way upto”.

So to summarize, Starting from letter 6 and all the way upto the end.

In the below second case, starting from beginning and all the way upto and end till letter “l”, this will output “travel”

You can also try like this as below, string[1:12:2] is [start:stop:stepcount]

Strings are immutable. You cannot assign a variable assignment and you cannot change it. you only have to concatenate like this

Collection Data types in Python

Lists: Ordered sequences, Mutable (values can be changed), Duplicates are allowed. It can be sorted.

Tuple: Ordered sequences, Immutable(values cannot be changed), Duplicates are allowed, It can be sorted.

Note: you need an ordered data type to represent sorted values

Set: Unordered sequences, unindexed and Every element in a set is unique (no duplicates). The set itself is mutable. We can add or remove items from it.

Dictionary: Unordered sequences, Mutable, Indexed, Duplicates are not allowed.

Lists in python

Lists are ordered sequences that can hold a variety of object types

Ordered sequence can be indexed and sliced

Lists can be nested

'list' object is not callable. so you cannot use ()

Below is the concatenation of lists

You can also change the values in lists. It is mutable

In the below command, I have changed the string object of “1” to “one”

Append method, appends to the end of the list.

I have added “six” and it appends at the end.

The below pop method removes the object from end of the list

You can also remove particular object in the lists by passing the index number.

Using sort method you can sort the objects

Dictionaries

Dictionary uses an unordered mappings for storing objects.

Dictionary uses a key value pair

Dictionaries are identified by curly braces and colon as seen below

{‘key’:’value’}

The elements in a dictionary are indexed by keys. Keys in a dictionary are required to be unique.

Dictionaries cannot be sorted. But Lists can be sorted.

In this case, you can store list inside a key.

If you want to grab the value of the list, you have to pass the ‘key’ to get the value.

'dict' object is not callable. So you cannot use ()

If you want to get only the value ‘2’ without the entire list. You can do this by passing index number.

You can also change the values by doing this.

In this case, I have changed the value of ‘2’ to ‘two’.

You can also specify if you want to return the keys and values by doing below

Tuples

Tuples are similar to lists but they are immutable (cannot be changed).

A tuple is an immutable sequence data type.

It does not support item assignment.

Tuples uses parenthesis. (1,2,3).

In the below, you can count how much time the object ‘a’ is present using count method.

If you try to modify the value in the tuple, it will result in error as seen below.

Set

Sets are unordered collection of unique elements.

It means you can have only one same object.

Sets uses curly braces. {1,2,3} (Doesn’t contain key value pairs as in the dictionaries).

In the below example, you can see that at first time added ‘4’ it got added, when I tried to repeat again by adding again, It keeps only one unique value.

The add() method adds an item to the set. The remove() item removes an item from a set.

In this example, I have casted the list to a set. Since it is casted to a set, Result shows only the unique values.

File I/O

In this example, I have a file created with these lines in notepad and saved in the same location where python is present. The saved file name is “demo.txt”

This command opens a file and reads it. You can see that “\n” is the new line.

Now if you try to read again, then you can see that it’s empty.

It is because cursor came to the end of the line. To reset the cursor , you need to pass the method “seek(0)” to reset.

Now if you read it again, it displays from beginning.

If you want to open the file in other locations, then keep in mind that,

To open the files in windows location use the double "\" so that it will not treat as the escape character.

“C:\\Users\\ramji\\AppData\\Local\Programs\\Python\\Python36\\test.txt”

To open files in the linux or mac location,

“/Users/ramji/folder/test.txt”

In this case, this is the new way of reading the file, you can see that there is “mode=r” it is the permission that you can set it to the file.

You can set “r” for read and “w” for write and “r+” for read and write.

Comparision operator and Boolean

In this you can use use logical operator “and” for comparing both condition to be true and “or” for either one to be true and “not” specifying not true.

If else statements

This is how the if else statements should be written.

a = 200 b = 33 if b > a: print("b is greater than a") elif a == b: print("a and b are equal") else: print("a is greater than b")

For loop

In our case, I have a variable named “list1” in which it has to be iterated.

I have create another variable “var” in which iterable will be stored. You can create any variable name.

Note that indentation(spaces) format in the print statement.

For Loop-Tuple unpacking

In this case, we have made tuple object inside the lists

we have defined variable as (a,b,c), we have to define exactly similar to that to unpack it and extract the values.

underscore variable in python

Underscore _ is considered as "I don't Care" or "Throwaway" variable in Python

The python interpreter stores the last expression value to the special variable called "_"

The underscore _ is also used for ignoring the specific values. If you don’t need the specific values or the values are not used, just assign the values to underscore.

for _ in range(50): do_something()

Method format

You can insert the formatted objects into strings inside the print statement

You need to specify only {}, so that the values will apply.

You can also specify index in the {} to apply according to the index as seen below.

Method Split

The split method will split the white spaces, also you can provide any value passed as a argument in split method.

It will provide the output as list.

While loop

In this case, while loop executes until the condition is satisfied.

Note: Don’t forget to enter the “x=x+1”, without entering if you try to get the output , it will become as infinite loop.

break – breaks or terminates the loop

continue – goes to the top of the loop

pass – does nothing

List comprehension

For example, Until now we were creating list logic as seen below,

With list comprehension, we can able to write lists in a single line and you can also flatten the lists as seen below.

Functions in python

A function is a block of code which is used to perform some specific actions. A function helps in code reusability.

Syntax

def function_name(parameters): #Block of code or statements

Parameter vs Argument

A parameter is a variable in a method definition.

When a method is called, the arguments are the data you pass into the method's parameters.

In this below, I have defined a function with “def” keyword and the name of the function is “add” and parameters are “x1,x2”.

Return keyword will output values when it is called.

Upnext I’m calling that function by passing the arguments(10,20).

These are called positional arguments and the reason is “10” is assigned to first position that is “x1” and 20 is assigned to second position that is “x2”.

Positional arguments are arguments passed to a function in correct positional order.

If I call it then it will return the output. And next I’m storing it in “result” and now if I call that object it will provide same output.

what is *args and **kwargs

In the below example , *args is useful in case you need multiple positional arguments.

In this case I have defined parameter *args and given multiple arguments by calling function “demo” and it returns as a tuple.

Below is the example of kwargs (keyword arguments).

This behaves the same but it returns as a dictionary. So that you can play around in your functions.

Note: you can also combine args and kwargs in a function also.

map in python

The map syntax is

map(func, iterables)

function - map passes each item of the iterable to this function.

iterable - iterable which is to be mapped

As seen below, It will check whether the numbers in the list are even are not.

“number%2 ==0” this means that if number modulus 2 is equal to 0 then it is even number else it is odd number.

Filter in python

Filter syntax is same as the map syntax.

I’m using the same function which we discussed above to show the differentiation.

In this case, I’m replacing the “map” to “filter”. The results are showing the even numbers only.

Filters the original iterable and provides the value for the items that returns True for the function provided to filter().

map() on the other hand will apply the provided function to each element of the iterable and return a list of results for each element.

Lambda Expression

Lambda is called as anonymous function, you don’t have to pass a function name.

Let us take an example below.

you can write lambda expression as below in a concise way.

Classes, Object and Attribute

To define a class in python, we need to use the ‘class’ keyword.

Class contains attributes and methods to execute on that data. Let’s take an example.

In the below code, I have created a class using “class” keyword.

There are two member variables of the class ‘name’ and ‘age’.

Every time we declare an object of this class, it will contain these two variables as its member.

you can see that “__init__” method is the constructor of the class.

Meaning, it will get called automatically when you create instance of the class.

“self” keyword represents the instance of the class.

By using the “self” keyword we can access the attributes and methods of the class in python

class Person: #initializing the variables name = "" age = 0 #defining constructor def __init__(self, personName, personAge): self.name = personName self.age = personAge #defining class methods def showName(self): print(self.name) def showAge(self): print(self.age) #end of the class definition # Create an object of the class person1 = Person("Ram", 27) #Create another object of the same class person2 = Person("Divya", 26) #call member methods of the objects person1.showAge() person2.showName()

attribute calling vs method calling

You can access attributes by calling class name and attribute name.

For example,

person.name

With method calling you need to have “()” parenthesis.

For example,

person1.showAge()

Inheritance

Inheritance inherits the methods and derives from the parent or base class.

Let’s see an example below.

As you can see below, The ChildA() and ChildB() derives the base class. So that when it is called, Base class gets executed.

Actual difference between the following 2 child classes is super() keyword. super() lets you avoid referring to the base class explicitly, which can be nice as seen in ChildB().

Note that the syntax changed in Python 3.0: you can just say "super().__init__()" instead of "super(ChildB, self).__init__()".

class Base(object): def __init__(self): print "Base class created" class ChildA(Base): def __init__(self): Base.__init__(self) class ChildB(Base): def __init__(self): super(ChildB, self).__init__() ChildA() ChildB()

__name__ == ‘__main__’

In certain classes in python, you might notice the built in variable “__name__”.

This will allow you to see whether the python source file is run directly by the interpretor or it is imported.

If this file is being imported from another module, then __name__ will be set to the module’s name.

Exception Handling

When you have multiple script in a code, when one script fails then the entire code will stop.

To refrain from that, error handling is useful.

When error handling is defined, the other script will get executed even if one script has an error.

Three keywords for error handling is defined below

Try: The block of code that might get error should be placed here.

Except: it will execute when “try” block has an error.

Finally: it will execute regardless of the error

In the below example. I tried adding a string to a number by creating an error so that the except block gets executed.

Decorators

A decorator takes in a function, adds some functionality and returns it.

In this example, you will learn how you can create a decorator and why you should use it.

I have created new_func() inside original_func() and new_func() will return their values if it is called.

Now let’s see what really decorators does with the @ symbol.

When you put the keyword “@original_func” on the top of another_func(), another_func() will be passed as an argument to the original_func and will be executed when the function is called.

So, @decorator is just an easier way of saying another_func = original_func(another_func). It’s how you apply a decorator to a function.

In the below case you can see a function being returned with values upon executing.

Generators

A generator-function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return.

It can also send back a value and then later resumes to pick up where it left off.

In the below example, I’m creating a for loop until the range is 5. You can see the keyword “yield”. It will not store the value in the memory and cause performance issues. It just yields it.

Once I call the generator() function, you can see that generator object generated at some location.

Now I’m creating a new instance of generator() and assigning a variable “gen”.

When I print the next(gen), it returns the first value of the function.

When I print again the next(gen), it will remember the previous value and provides the next value.

It will not store it in the memory location.

Collections

Collections are known to store collections of data.

Beyond the built-in data types such as (list, dict and tuple), Python collections also contains specialized data structures to store collections of data.

Let’s see one by one below!

a.) OrderedDict

An OrderedDict is a dictionary subclass that remembers the order that keys were first inserted.

OrderedDict track the order in which the keys are inserted.

A regular dictionary doesn’t track the insertion order.

Below is a regular dictionary and note the values which is not in correct order.

x = {} x['a'] = 1 x['b'] = 2 x['c'] = 3 x['d'] = 4 x['e'] = 5 for key, value in x.items(): print(key, value) Output is ('a', 1) ('c', 3) ('b', 2) ('e', 5) ('d', 4)

Below is an orderedDict which outputs in correct order.

from collections import OrderedDict od = OrderedDict() od['a'] = 1 od['b'] = 2 od['c'] = 3 od['d'] = 4 od['e'] = 5 for key, value in od.items(): print(key, value) Output is ('a', 1) ('b', 2) ('c', 3) ('d', 4) ('e', 5)

b.) Counter

counter is a dict subclass which helps counts hashable objects.

Inside of it the elements are stored as dictionary keys and counts of the object are stored as value.

In this example you can see that, it counts all the elements in the list and it displays count of each element as a value in the form of dictionary.

c.) Regex

In this example you can see that, I have imported the regular expression.

[^!.? ]+ means that it doesn’t match the symbols “!.? and space”.

^ (carat) sign will exclude everything that is followed by not “!.? and space”

I have added the + sign to check that match appears at least once.

As you can see below that (re.findall) excludes the symbols that are present in the in “word” variable and breaks into a separate words.

Json files parsing and extracting values in python

Below is an example json where we are going to slice and dice to extract the values.

for k,v in data.items(): print k print v['address']['addressLine1'] print v['address']['addressLine2'] print '' output: John Out in the sticks Somewhere