How to use the python function to repeat the task?

Image credit: towarddatascience

Doing the same task is boring. Also, the same thing in python is boring. So what’s the solution?

The conventional solution could be to do the program again and again but it becomes frustrating.

So, you can use the python function. You will tell me what’s the python function?

How can I automate the repeated task?

I’m going to show you what’s the python function? How can you use it? And what’s the benefit of using it?


What’s the python function?

The python function is a process where you can do repeated tasks easily. For example, passing numbers through function.

Whenever you want to repeat a task you can just call the function.

So, your next question how it looks like?

The appearance of the python function shows

def function_name(args):

In the above syntax you can see that I have started the function with def keyword.

The meaning of the def keyword is to define the function name.

After the def keyword, you can see that I have passed the arguments with parenthesis and at last semicolon(:).

Remember, it’s the starting point of function, not the whole structure of the function.

When you move forward in the blog you understand the structure of the python function.


Use of docstring

You will be amazed,” I have known about string”. But What’s the docstring?

Don’t be amazed it’s the element of the python function which explains what are you doing in function?

It looks like:-

def function_name(args):
""" what the
function doing"""

You can say, docstring in function is similar to comment in python.

Remember, docstring use triple quotes for comment.

Till this point I have define function name, argument and docstring.

The next point I’m going to show you is the statement or output which going to display on the computer screen.


Return statement in function

The return statement returns the statement after calling the function.

It ends the execution If the function calls and returns the result.

For example,

Output:

If the return statement is without any expression then the special value none is returned.

For example,

Output:

You can see that If there is no expression in the return statement result will be None.

So, you would say why the None value?

None value means no expression in the return statement.


Passing the value

You can pass the value from a function to display the output with the passed value.

For example,

Output:

In here, you can see that I have passed the number 6 and the result has come.

You can also pass the value without a return statement. But output will display with a statement.

For example,

Output:

In the above example, you can see that output has come with a statement.

Arbitrary and keyword argument

In the next part of the blog, you are going to see what’s the arbitrary and keyword argument.

So, let’s see how can you passed the value in different way.


When number of arguments are not fixed!

Suppose, In a function, you don’t know how many values or arguments are going to pass. You can use arbitrary keyword.

You will be amazed what’s the arbitrary keyword?

You are going to see what’s it?

The arbitrary keyword is an asterisk(*) sign with arguments that permit you to pass as many arguments as you want to pass.

You can relate to this example.

For example,

Output:

In the above example, you can see that I have displayed the two numbers in a different statement.

Remember, the arbitrary argument is accessed using indexing.


Pass the argument as key-value pair!- Keyword argument

You can use a keyword argument to pass the key-value pair from the function.

For example,

Output:

So, you would say what’s the benefit of keyword argument.

If you clearly see that I have given first position to num1 even in function num position is first.

What’s that mean?

That means you don’t care about the order of the argument. You can put in any order with the help of the keyword argument.

In the above example, you had seen how can you pass arguments in any order.


You don’t know how many keyword arguments- **Arbitrary keyword argument

You can see what I’m going to do in this section with the heading.

I’m going to show you what’s the benefit of an arbitrary keyword argument!

You can pass as many as keyword argument using **Arbitrary keyword argument.

You can use a double-asterisk(**) sign to indicate this.

For example,

Output:

In the above example, you can see that I have passed multiple keyword arguments using ** double-asterisk.

How can you display value of the key?

You can display the value of the key using the key in the print statement.


Pass statement in python- make an empty function!

You can use the pass statement in the python function if you don’t want to execute the function.

Perhaps, you would say, ” We will not give print or return statements”.

You thought right but It will give an error.

For example,

Output

In the above example without a pass statement, it has given a syntax error.

You can use a pass statement in to prevent errors.

For example,

Output

In the above example, you can see that when I have used the pass statement no error has came.


Conclusion

You can repeat the task in python using function.

Function taking arguments to return value. These values are returns using the return statement.

You can use arbitrary arguments to return as many as the value you want.

Using keyword argument you don’t need to care about the order of function argument.

So, what do you think about the python function? Is there anything I have not covered? And you want to cover, comment on it!

I hope you feel it!


For more: visit

Why python slicing is
important?
Create a list with
short syntax!
Core values of python!


"Quality brings clarity "

Why python slicing is important?

You had known about Indexing. In this blog, you are going to see the slicing in python.

The slicing in the python is the portion extract from the iterable. Iterable are string,list, tuple, set and dictionary of sequence.

These sequences use a square bracket([]) for slicing.

The syntax for slicing is iterable[start:end].

The start is the position where slicing start.

The end is the position where the slicing finish.

Image credit: Railsware

Remember, slicing is not possible in the dictionary and set because of unordered and immutable behaviour.


The start of slicing

The start of slicing is the position where the sequence starts.

You can give any number to start position. For example,

Output:

In the above example, you can see that I have used string iterable for slicing.

You can use any iterable for slicing.

For example,

Output:

In this example, you can see that I have used slicing in the python list.

Remember, The first element position in the sequence is always 0.

For example, in this case, the first element 1’s position is 0.


The end of slicing

At the end of the slicing, the sequence is finished. It’s the last position of the sequence.

For example,

Output:

In the above example, you can see that the end position is 3.

If you carefully observe then you will found that the last element is last-1. In this case, it’s ‘l’ in 2nd position.

Remember, The last position in slicing always will be last-1 because of the Mathematical formula.


Starting position not included

Suppose in slicing starting position is not included. Only the last position in slicing is included.

What do you think will happen?

For example,

Output:

The purpose is to show how code is working.

In the above position, you can see that starting position is not included.

So, the sequence has started from the 0th position.

Here, you can see that the 0th position element is ‘h’ till the given 3rd position.

Remember, when starting position is not included the sequence start with the 0th position of iterable.


End position not included

When the end position is not included in slicing then what you think will happen?

It will look like [start: ].

For example,

Output:

In the above example, you can see that the last position is not included only the first position is included.

You can see that the sequence from starting position till the end has come.

Remember, when the last position is not included then from the start position till the last position sequence will come.


Negative indexing

Image credit: python-tricks

You can slice the python sequences using Negative indexing.

The Negative indexing starts from the last of the sequences.

For example,

Output:

In the above example, you can see that sequence starting from -4 I.e. 2nd element and ending at the last element.

In this case, The last element ‘l’ is -1 in negative indexing.


Copy the list using a slice

Suppose, you have to copy the list using a slice. The new copied list should not affect the old one. What do you think you can do?

For example, you have to create a new list of a number using the old one.

Output:

In the above example, you can see that I have created a new list of new_numbers using slicing.

Also, you can see that when I have made changes in the list new_numbers, it doesn’t impact the old one.


Conclusion:

Python slicing is important to get some parts of iterable. the iterables are string, tuple and list.

Remember, slicing is not possible in the dictionary and set.

You can also slice using negative indexing. Slicing is a very powerful tool to create a new list.

You can also create a new list using a slice in copy. If you make any changes to the new list, the old one will not be impacted.

Have you found it useful? What do you think? What points I have missed, You think you can cover?



For more, you can visit

List-Comprehension
Python-object
Python-Inheritance
Python-class
Python-lambda

"Quality brings clarity"

How everything is an object in python?

Image credit: AnalyticsVidhya

In recent times, me and my friends thinking about why everything is an object in python?

To know more about we have done the research and finally found it.

I can guess you also want to know why everything is an object in python?

What features and quality made the python object-oriented programming language?

To know more about why everything is an object in python? You have to find what is an object?

The object has a different definition in every programming language.

You are going to see what is object?


What is an object in python?

Object

In python, the object is a component of a program that holds both data(variables) and methods(function).

For example, In the factory, 🏭 the component of the system, holding raw material and action in assembly lines is called an object.

In this example, the raw material is data and the assembly line is methods.

Sometimes in the place of a method python could hold attributes.

For example,

Output:

In the above example, you can see that I have applied the method type to the data of x.

The most interesting part is that class and type both synonymous with each other.

In this example, you can see that an str is an object which holds both data and attributes.


Everything is an object

In object-oriented programming languages like python, the object is a component that holds data and functionality together.

In python, everything is an object that means every component has data(attributes) and action(methods or functions).

These data and methods can access via dot(.) Syntax.

For example,

Output:

List

In the above example, you can see that I have worked on variable x with the help of remove method.

Also, access the attribute with the help of dot(.) Syntax.

When I say everything in Python is an object, I mean it.

Even the attribute and methods in object are also objects.

For example,

>>>x = 5.0
>>>x.is_integer()
True
>>>type(x.is_integer())

builtin_function_or_method

In the above example, you can see that data x and method is_integer is also objects themselves.


Why everything is an object?

Everything is an object that gives great benefit to using python. The objected-oriented created great advantages.

For example,

1. Easy troubleshooting

Object-oriented programming gives easy troubleshooting that means if the problem found in some parts you have not to look anywhere.

You can attack that part and solve it.

2. Reuse code through Inheritance

You can reuse the code in python with the help of Inheritance.

Inheritance is building new in the base of old code.

For example, you have to build a new car tesla in the base of Honda.

There are some commonalities in both car but you have to build a tesla car.

For more, you can visit my blog python-inheritance on reusable code.

3.Data redundancy

With the help of the data redundancy, you can store the same piece of data in a two different place

For example, the sale record of a salesman. In this case multiple sales of the same salesman store in a different place.

But the name of the salesman is the same in both entries. It’s data redundancy.

4. Make a security

With the use of data hiding and abstract, you can secure the data in python. So data will not be visible to attackers.

5. Effective problem solving

Suppose, you have to solve the problem. So, you don’t check the whole program.

Simply you can break the program into small pieces and solve the problem in a specified area without affecting another part of the program.

The great benefit of OOP(object-oriented programming) is that it does affect the whole program to solve the problem.


This all advantage of OOP makes the reason you should use python 🐍 programming language.


Conclusion:

Everything in python is object means every data and methods are associated to each other.

With the help of the OOP(object-oriented programming), you can store same piece of data in different places.

In python, you can group similar things.

You don’t need to keep track of little things in python because Everything is in python is an object.

There are many advantages of OOP(object-oriented programming) which increase the productivity of developers.



I hope you loved this blog. If you found something misinform you can comment on it.


For more, you can visit

python-inheritance
python-class
python-intro

Also, you can


"Quality brings clarity"

Python loop- Come to a sequence of data!

Image credit: wikipedia.com

In the computer lab, my friend and I doing a repeated task. Then we thought is there any method to do this task easily?

We found something amazing! That was a python loop.

Python loop repeating a task till condition doesn’t satisfied. It’s like the automation of tasks.

The second question is How can you use a loop? What’s the type?

You can use the loop with two methods:-

• for loop
• while loop

1. Python- for loop

Suppose, you want a repeated sequence of elements. How will you do it?

You can do it with for loop. The for loop giving a particular sequence of elements.

The for loop can be used in list, tuple etc.

For example,

Here, you can see that the list elements have come in sequence.

Suppose you have to stop at number 20 in for loop and wants to display a message. How will you do it?

You can do it with if conditions.

For example,

Here, you can see that the first number 1 passed through for loop. It doesn’t match to if condition so it directly printed.

When 2nd number 20 passed through for loop. It catches the if condition and display the if message.

After, displaying if ‘s message, it will display the number 20.

The third time it will pass the number 45. It directly prints because it doesn’t satisfy the if condition.

for loop for string

Here, you can see that string “hello” has come in sequence. The string can come in sequence.

for loop in range() method.

To know how to use the range() method in for loop. First, you have to understand how the range() method works?

For example,

Here, you can see that number has started from 0 and end at last-1 I.e. number 3. Number 4 will not be included.

Finally, you had seen how a range method works!

Next, you are going to see how to use it in for loop.

For example,

Here, you can see that all the number has come in sequence.

Number 4 is not included. Because range() method does not include 4. The last element will be a last-1 number.

else method in for loop

Suppose, your loop has finished and you have to display the message for finished.

For example,

Here, you can see that after completing the sequence of number message has displayed.

Nested for loop

Suppose you need one loop inside another loop. The outside loop will be a row and inside the loop will be a column.

For example,

The outer loop is a row and the inner loop is a column.

First, the sequence started with the number 0 then inner it will go to numbers 0 and 1.

2. Python- while loop

The while loop executes as soon as the condition is true.

When the condition becomes false the loop will be stopped.

For example,

Here, you can see that number starts from 0 and end at number 2.

1. break statement in while loop

The break statement in the while loop breaks the sequence at a specific point.

Here, you can see that sequence has broken in number 2.

The sequence doesn’t reach number 3.

2. Continue statement in loop

The continue statement in for loop change the sequence at a particular point.

For example,

The continue statement change the current iteration and start a new one.

3. Else statement in while loop

The else statement in the while loop executes when the condition is no longer true.

For example,

Basically, In this section, you had seen that while loop executes till the condition is true.


Conclusion:-

Loops are used to automate the repeated task in python.

There are two types of loops in python. The first is for loop and the second one is while loop.

The for loop uses for a repeated sequence of elements. The while loop executes till the condition is True.

There are continue and break statements in python while loop.

Break statement in python while loop breaks the sequence of the elements.

Continue statement change the current sequence and start a new sequence.



"Quality brings clarity"

Python tuple – Share with care of data!

Image credit: trytoprogram.com

Recently, I have to share data with someone. But I have a fear of a manipulation of data.

So, I’m thinking about what should I do?

Finally, after some research, I have found that I can share with the care of my data using python tuple.

You can protect your data with python tuple.

Your next question is “what is a tuple in python?”

Let’s see what it is?

Python tuple

1.Tuple

A tuple is an ordered and unchangeable data type in python.

It is used to store the unchangeable data items in python.

2.How to create a tuple?

You can create a tuple with the help of (). The items are going to store inside the ().

Let’s see how can you do?

tuple of names

3.Tuple elements

In this section, you are going to learn:-

•Ordered
•Unchangable
•Allow duplicates
•Tuple with one item
•Tuple length
•Data type of tuple item

1) Ordered

Image credit:booster.io

When it comes to ordering. What do you understand by the order of python tuple?

In the case of a tuple, it’s the same as a list. Order means indexing of tuple items. The position of the tuple item.

The indexing of tuple item denoted by []. Let’s see how can you see it!

names are in order

The first name shows the order

2) Unchangeable (Immutable)

Immutable tuple
Image credit:radar.oreilly.com

What do you understand by unchanged tuple?

It means you can’t remove, add or change the size of the tuple. Tuple has a fixed size.

Try the append method

The output shows the fixed size of the tuple.

The same thing will happen for the remove method. That shows tuple is unchangeable.

Q. Why tuple is immutable?

Ans:- 1) Tuples are following the same sequential operation as a string. 2) Tuples are used for the order of data that’s why the tuple is immutable.

3) Tuple – Allow duplicates

Duplicate item
image credit:istockphoto.com

Suppose you need a duplicates item in a tuple. So, what you will do?

You will simply put the duplicate items. Let’s see how can you put duplicate items in a tuple?

Created duplicate items

“vipul” has come two times

4) Tuple items – Data type

Data type: tuple

In python programming, everything is an object. Suppose, you have to find the data type of tuple items. What you will do?

You can use the type() method.

type() method:-

With help of type(), you can find the data type of tuple items.

Let’s see how can you do!

type() method

type ‘tuple’

5) tuple() Constructor

tuple constructor
Image credit: codespeedy.com

Suppose you have stored your data as a list and you want to change it as a tuple. How will you do it?

You will use tuple() constructor. Let’s see how can you do!

Example:-

tuple created

6) Tuple length

len() method
Image credit: master programming.com

Suppose you have to find the length of the tuple or items present in the tuple. You will use the len() method in the tuple.

Let’s see how can you do!

Example:-

Number of items

7) Tuple with one item

Suppose you have one item in a tuple.

How will you represent one item in a tuple?

If you only represent with (“vipul”) then the data type of it’s become string.

Let’s see how it’s going to see!

Example:-

Data type ‘str’

The right approach to do this is using a comma after first item(“vipul”,). You will see the data type has changed to ‘tuple’.

You Can code:-

Example:-

Type ‘tuple’

In this part, you have seen how comma(,) is changing the data type.

A comma at the end of the item shows the data type ‘tuple’.

You have seen ordered, unchangeable, duplicates, length and type of tuple.

Also, shown why we used a tuple!

Tuple has interesting behaviour of privacy of data.

You can use a tuple to make the data as secure as you want!

Conclusion:-

Tuple and list have many similarities. But some differences make a tuple faster than a list.

Because of fixed size tuple taking less memory than a list.

Tuple’s unchangeable behaviour makes tuple use in sharing data.

Python List-Store as much as you want?

In recent days my friend and me storing our name on each new variable. It taking a lot of times to store each one of the names in a new variable.

So, we decided to move to something easy and flexible. Our friend group is aware of the python list.

We had used a python list to store each of the names in one variable. It’s great!

You have finally found it. You can store multiple items in one variable. No need to create more variables.

The misconception is that every new variable is clear and concise. It’s true but in the Python list, we can do it easily.

So you are going to see how to store multiple items at once.

First, you will see what’s the list?

Why we’re using it?

What’s the benefit?

How to do it?

List

Python List is the ordered group of items store in one variable.

The list can be of any data type or mixed data type. You don’t care about the data type in the list.

Creation of list

We can create a list using square brackets.

The square brackets hold the multiple items of the list. It indicates by [].

List items

List Items are ordered, changeable and allow duplicate items in the list.

Ordered

List items are ordered. You can use [] to check the order of list.

The first items start with [0]. The number inside [] brackets is called indexing. Indexing gives the particular item to you.

Changeable

You can change the item with the remove, append method In the list.

Allow duplicate items

The python list allows for duplicate items. Duplicate items are allowed because of different index number.

List Length- To Find Number Of Items

Suppose you have to find the number of items in the list. The traditional method is to count every item.

But If there are thousands of items. So it will take more time to count. To make it easy you use the len() method in the list.

List items – Data types

The data type of list items can be mixed. In one list there are many different data types.

Range of Indexes

The search starts from the first given number to the second number. It uses [start:end].

The return value is a new list. In the list some items of the old list are present.

The indexing always starts from 0.

The element at index 0 is the first item.

Range of Negative Indexes

If you want to search items from the last of, then you can use negative indexing.

The last element index in negative indexing is -1.

The last number is not included. In this case, -1 has not included.

The last number would be last-1.

Here, it is -1-1=-2.

type() method in list:

In python list considered as an object. Suppose you have to find the data type of list, you can use the type() method.

List Constructor- Creating a list

You can also create a list without [] brackets with a list() constructor.

Check if items exist

To find if the item is present in the list we use in keyword.

if present it will print a message.

Conclusion

Python list is ordered, changeable and allow duplicate items. The data type of list is <class ‘list’>. You can use a python list to store the school registration form of students. You have not to create a separate variable for each student. We can access a particular item in the list with the help of an index number.

See also:

PythonOperators

https://vipulkunwar503.code.blog/2021/06/19/python-operators/

PythonSyntax

https://vipulkunwar503.code.blog/2021/06/13/python-syntax/

Python Is In sky !

1.Python Graph

Growth of python

      Python increasing fastly in the world.  The python has a large scientific library. 

      It has overtake the Java and c programming language in 2020 . It’s  highly compatible with any other programming languages.

      The core value of python is simplicity. The people can see how this impacting the world.

      The python programming language take less step than any other programming language. So,people preferring it most.

      Python is creating large space in Data science field. The automation is largely doing by python programming language.

2.The companies shifted toward python language.

1.The Reddit

      The Reddit shifted itself toward python language. 

      The CEO Steve Huffman said,” The Reddit is going to shift toward python because of large library which give us to the compabality “.

     Reddit is 4th largest website in United States. With users of 320million ,it’s increasing fastly.

2. Netflix

     Netflix is mainly build on python programming language .

     It is highest Entertainment  sector .Which mainly use user interface to see it.

3. Google search engine

     Google search engine basically uses python programming language for analytics in search result. The Google has automate the search engine system through python language.

3. Python blogging Industry.

      Python blogging Industry increasing very fast. The bloggers and IT professionals creating the blog to reach the large audience.

    There are some best python blogging Channels are Real python,Python Foundation.

4. Python using in Digital marketing

     Digital marketing is new form of marketing, where companies are putting there product online.

     The Digital marketer are doing some repeated task . To automate this digital marketer started the python data science.

      Python has large scientific libraries. Eg. numpy, scipy, torchpy etc.

     So,the people who are thinking digital marketing is only about marketing aspects it’s not true. although the python is small part till now.

Python growth

Conclusion:- The python is increasing fastly.

The child and young age preferring python over another programming languages.

It giving users less steps to do things,large scientific library.

If this progress remain consistent, then python will overtake SQL Language.

Python Generation

After C,C++ and Java python programming changing the world ,how we see the coding. So,why we are talking about python generation ?

Python in US, the 5th grade students are learning. The teacher s making it interesting through quizzes,games,problem solving approaches. So, I ask to many people what can we do ?

In india we can introduce early. Make a new system to learn programming,not make only boring logical stuff. The reason behind the lack of coding language is that we don’t know what we need through this stuff. Python has large library support, easy syntax and easy to adapt . We can make great web structure from python .

We can learn programming through fun game making language. Python has a facility of pygame in through which we can create game.

Python is changing the world,it has added to Google search engine. Netflix is using it largely. It is gaining large access to corporate world. In this generation kids are growing with great technological advancement. They are using Python as theire primary programming language to learn.

Python also facilities with great framework,Just like django,numpy etc. I recommend you to study the book python for kids. I know it’s for kid,but it giving great insight of python programming language. It is not only for python. It’s about your logical thinking and intuition how you can adapt change easily.

In last time I can say the world is moving fast. In field of Computer science everything changing fast. If we have to move forward we have to change fastly.

I hope you like this . If there is any mistake you can comment on it,I feel helpful about this. If you have anything interesting you can tell on comment .

Working with Files

♤Write Method

Today,we are going to see how to write in python file. Write in file uses “w”. Syntax of write method. file.write(),write take one display statement.

■■greeting.py

"Hello, world!"
________________________


So,question is how we will do it ?

1.Open the file.
2.Read the file.
3.Close the file.
4.Open the file in write
Mode.
5.use write method.
6.Inside write method,
Whatever content we
want to write.
________________________
________________________
Note: Write method
Overwrite the
file. The old content will erased.
________________________
________________________

■■display.py

f = open("greeting.py",
"r")

with open("greeting.py")
as f:
print(f.read())
f.close()


f = open("greeting.py",
"w")
with open("greeting.py")
as f:
print(f.write("Good
","morning")


Output:

Good,morning

♧append method :

Append method in a file add content to file. Open the file in append mode.

■■Hello.py

"Hello,world ! "

■■go.py
# open the file in
# append mode.

f = open("Hello.py","a")
f.append(" Bob and Alice.")

print(f)

Output :

Hello,world Bob and Alice.

________________________

Note: Append method add
the content in existing file content.
________________________

Inheritance in python

     Python program to demonstrate Inheritance.  so,basically what is Inheritance?

     Inheritance give permission to user to not to start program  from scratch. The first class attribute can be come with help of Inheritance . Example, the first class is car, and second class is ElectricCar .the all car properties can come with help of Inheritance. In this car class is parent class,ElectricCar class is child class.

car.py

class Car():
    """Intialization
of car attributes
"""                 
    def __init__       (self,name,
model,year):
  
      # access the car
  # attribute
      self.name = name
      self.model = model
      self.year = year

    def describe_car(
        self):
        """print the
information    
        about car"""

       
print(self.name+" 
       "+self.model+" "
       +str(self.year))

class ElectricCar(Car):
      """Intialization
of car attributes
"""
      def__init__(self,
name,model,year):
"""Evoking the
class
attributes """
super().__init_
_(name,model,
year)

def display(self):
"""display
ElectricCar
model"""
print("Model:"+
self.model)

# creating a ElectricCar
# object

Vehicle = ElectricCar(
"tesla","S1M",
2016)

print(Vehicle.name)
Vehicle.describr_car()


Output:

tesla
tesla S1M 2016

Note: super() method
evoke the
attributes from
parent class to
child class.