Tag Python_basics

Python Dictionaries

A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values.

Example:
dict = {‘Name’: ‘Zara’, ‘Age’: 7, ‘Class’: ‘First’}
print “dict[‘Name’]: “, dict[‘Name’]
print “dict[‘Age’]: “, dict[‘Age’]

When the above code is executed, it produces the following result

dict[‘Name’]: Zara
dict[‘Age’]: 7

Let’s Explore more about dictionaries through jupyter notebook.

Python Sets

A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.

Example:

thisset = {“apple”, “banana”, “cherry”}

Access Items

You cannot access items in a set by referring to an index, since sets are unordered the items has no index.

But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in keyword.

Let’s explore Sets and Booleans concepts with Jupyter notebook.

Comparison Operators

In this lecture we will be learning about Comparison Operators in Python. These operators will allow us to compare variables and output a Boolean value (True or False).

If you have any sort of background in Math, these operators should be very straight forward.

First we’ll present a table of the comparison operators and then work through some examples:

Table of Comparison Operators 

In the table below, a=3 and b=4.

OperatorDescriptionExample
==If the values of two operands are equal, then the condition becomes true.(a == b) is not true.
!=If values of two operands are not equal, then condition becomes true.(a != b) is true
>If the value of left operand is greater than the value of right operand, then condition becomes true.(a > b) is not true.
<If the value of left operand is less than the value of right operand, then condition becomes true.(a < b) is true.
>=If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.(a >= b) is not true.
<=If the value of left operand is less than or equal to the value of right operand, then condition becomes true.(a <= b) is true.

Let’s now work in jupyter notebook through quick examples of each of these.

Python Statements

In this post we will be doing a quick overview of Python Statements. This post will emphasize differences between Python and other languages such as C++.

There are two reasons we take this approach for learning the context of Python Statements:

  • If you are coming from a different language this will rapidly accelerate your understanding of Python.
  • Learning about statements will allow you to be able to read other languages more easily in the future.

Python vs Other Languages

Let’s create a simple statement that says: “If a is greater than b, assign 2 to a and 4 to b”

Take a look at these two if statements (we will learn about building out if statements soon).

Version 1 (Other Languages)

if (a>b){
a = 2;
b = 4;
}

Version 2 (Python)

if a>b:
a = 2
b = 4

You’ll notice that Python is less cluttered and much more readable than the first version. How does Python manage this?

Let’s walk through the main differences:

Python gets rid of () and {} by incorporating two main factors: a colon and whitespace. The statement is ended with a colon, and whitespace is used (indentation) to describe what takes place in case of the statement.

Another major difference is the lack of semicolons in Python. Semicolons are used to denote statement endings in many other languages, but in Python, the end of a line is the same as the end of a statement.

Lastly, to end this brief overview of differences, let’s take a closer look at indentation syntax in Python vs other languages:

Indentation

Here is some pseudo-code to indicate the use of whitespace and indentation in Python:

Other Languages

if (x)
    if(y)
        code-statement;
else
    another-code-statement;

Python

if x:
    if y:
        code-statement
else:
    another-code-statement

Note how Python is so heavily driven by code indentation and whitespace. This means that code readability is a core part of the design of the Python language.

Now let’s start diving deeper by coding these sort of statements in Python!

Time to code!

Python for Loops

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.

With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

Here’s the general format for a for loop in Python:

for item in object:
_____statements to do stuff

The variable name used for the item is completely up to the coder, so use your best judgment for choosing a name that makes sense and you will be able to understand when revisiting your code. This item name can then be referenced inside your loop, for example if you wanted to use if statements to perform checks.

Let’s go ahead and work through several example of for loops using a variety of data object types. We’ll start simple and build more complexity later on.

Python while Loops

The while statement in Python is one of most general ways to perform iteration. A while statement will repeatedly execute a single statement or group of statements as long as the condition is true. The reason it is called a ‘loop’ is because the code statements are looped through over and over again until the condition is no longer met.

The general format of a while loop is:

while test:
    code statements
else:
    final code statements

Let’s look at a few simple while loops in action.

Python List Comprehensions

Python is famous for allowing you to write code that’s elegant, easy to write, and almost as easy to read as plain English. One of the language’s most distinctive features is the list comprehension, which you can use to create powerful functionality within a single line of code. However, many developers struggle to fully leverage the more advanced features of a list comprehension in Python. Some programmers even use them too much, which can lead to code that’s less efficient and harder to read.

By the end of this tutorial, you’ll understand the full power of Python list comprehensions and how to use their features comfortably.

Benefits of Using List Comprehensions

  • One main benefit of using a list comprehension in Python is that it’s a single tool that you can use in many different situations.
  • In addition to standard list creation, list comprehensions can also be used for mapping and filtering. You don’t have to use a different approach for each scenario.
  • List comprehensions are also more declarative than loops, which means they’re easier to read and understand.

Every list comprehension in Python includes three elements:

  1. expression is the member itself, a call to a method, or any other valid expression that returns a value. In the example above, the expression i * i is the square of the member value.
  2. member is the object or value in the list or iterable. In the example above, the member value is i.
  3. iterable is a list, set, sequence, generator, or any other object that can return its elements one at a time. In the example above, the iterable is range(10).

Let’s explore the concept through jupyter notebook.

Python Function

A function is a block of code which only runs when it is called. Python allows us to divide a large program into the basic building blocks known as function.

The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the python program.

Python provide us various inbuilt functions like range() or print(). Although, the user can create its functions which can be called user-defined functions.

Advantage of Functions in Python-

There are the following advantages of Python functions:

  • By using functions, we can avoid rewriting same logic/code again and again in a program.
  • We can call python functions any number of times in a program and from any place in a program.
  • We can track a large python program easily when it is divided into multiple functions.
  • Reusability is the main achievement of python functions.
  • However, Function calling is always overhead in a python program.

Creating a function –

In python, we can use def keyword to define the function. The syntax to define a function in python is given below.

def my_function():
function-suite
return <Expression>

Function calling –

In python, a function must be defined before the function calling otherwise the python interpreter gives an error. Once the function is defined, we can call it from another function or the python prompt. To call the function, use the function name followed by the parentheses.

A simple function that prints the message “Hello Word” is given below.

def hello_world():  
    print("hello world")  
hello_world()   

output-
hello world

Parameters in function –

The information into the functions can be passed as the parameters. The parameters are specified in the parentheses. We can give any number of parameters, but we have to separate them with a comma.

Consider the following example which contains a function that accepts a string as the parameter and prints it.

Example-

#python function to calculate the sum of two variables   
#defining the function  
def sum (a,b):  
    return a+b;  
  
#taking values from the user  
a = int(input("Enter a: "))  
b = int(input("Enter b: "))  
  
#printing the sum of a and b  
print("Sum = ",sum(a,b))  

Output-

Enter a: 10
Enter b: 20
Sum = 30

The return Statement –

The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

All the above examples are not returning any value. You can return a value from a function as follows

# Function definition is here
def sum( arg1, arg2 ):
   
# Add both the parameters and return them."
   total = arg1 + arg2
   print "Inside the function : ", total
   return total;

# Now you can call sum function
total = sum( 10, 20 );
print "Outside the function : ", total 

Output-
Inside the function :  30
Outside the function :  30

Types of arguments in the function –

There may be several types of arguments which can be passed at the time of function calling.

  1. Required arguments
  2. Keyword arguments
  3. Default arguments
  4. Variable-length arguments

Required Arguments-

Till now, we have learned about function calling in python. However, we can provide the arguments at the time of function calling. As far as the required arguments are concerned, these are the arguments which are required to be passed at the time of function calling with the exact match of their positions in the function call and function definition.

If either of the arguments is not provided in the function call, or the position of the arguments is changed, then the python interpreter will show the error.

Consider the following example.

#the argument name is the required argument to the function func   
def func(name):  
    message = "Hi "+name;  
   return message;  
name = input("Enter the name?")  
print(func(name)) 

Output-
Enter the name?John
Hi John

Keyword arguments –

Python allows us to call the function with the keyword arguments. This kind of function call will enable us to pass the arguments in the random order.

The name of the arguments is treated as the keywords and matched in the function calling and definition. If the same match is found, the values of the arguments are copied in the function definition.

Consider the following example.

#function func is called with the name and message as the keyword arguments  
def func(name,message):  
    print("printing the message with",name,"and ",message)  
func(name = "John",message="hello") #name and message is copied with the   values John and hello respectively  

Output-
Printing the message with John and  hello

Variable length Arguments –

In the large projects, sometimes we may not know the number of arguments to be passed in advance. In such cases, Python provides us the flexibility to provide the comma separated values which are internally treated as tuples at the function call.

However, at the function definition, we have to define the variable with * (star) as *<variable – name >.

Consider the following example.

def printme(*names):  
    print("type of passed argument is ",type(names))  
    print("printing the passed arguments...")  
    for name in names:  
        print(name)  
printme("john","David","smith","nick")  

Output:
type of passed argument is  <class 'tuple'>
printing the passed arguments...
john
David
smith
nick

Default Arguments –

Python allows us to initialize the arguments at the function definition. If the value of any of the argument is not provided at the time of function call, then that argument can be initialized with the value given in the definition even if the argument is not specified at the function call.

Example-

def printme(name,age=22):  
    print("My name is",name,"and age is",age)  
printme(name = "john") #the variable age is not passed into the function however the default value of age is considered in the function 

Output:
My name is john and age is 22

Scope of variables –

The scopes of the variables depend upon the location where the variable is being declared. The variable declared in one part of the program may not be accessible to the other parts.

In python, the variables are defined with the two types of scopes.

  1. Global variables
  2. Local variables

Variables that are defined inside a function body have a local scope, and those defined outside have a global scope.

This means that local variables can be accessed only inside the function in which they are declared, whereas global variables can be accessed throughout the program body by all functions. When you call a function, the variables declared inside it are brought into scope. Following is a simple

example –

total = 0; # This is global variable.
# Function definition is here
def sum( arg1, arg2 ):
   # Add both the parameters and return them."
   total = arg1 + arg2; # Here total is local variable.
   print "Inside the function local total : ", total
   return total;

# Now you can call sum function
sum( 10, 20 );
print "Outside the function global total : ", total 

When the above code is executed, it produces the following result −

Inside the function local total :  30
Outside the function global total :  0

Recursion:

Python also accepts function recursion, which means a defined function can call itself.

Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result.

The developer should be very careful with recursion as it can be quite easy to slip into writing a function which never terminates, or one that uses excess amounts of memory or processor power. However, when written correctly recursion can be a very efficient and mathematically-elegant approach to programming.

In this example, tri_recursion() is a function that we have defined to call itself (“recurse”). We use the k variable as the data, which decrements (-1) every time we recurse. The recursion ends when the condition is not greater than 0 (i.e. when it is 0).

To a new developer it can take some time to work out how exactly this works, best way to find out is by testing and modifying it.

Example –

deftri_recursion(k):

  if(k > 0):

    result = k + tri_recursion(k - 1)

    print(result)

  else:

    result = 0

  return result



print("\n\nRecursion Example Results")

tri_recursion(6)

Output-

Recursion Example Results:
1
3
6
10
15
21

In the next chapter we will go through several function examples

Reference

W3school

Tutorialpoint

Javapoint