Found 10805 Articles for Python

Custom len() Function In Python

Hafeezul Kareem
Updated on 30-Jul-2019 22:30:26

655 Views

Let's see how can we implement custom len() function in Python. Try it by yourself first using the following steps.StepsGet the iterator from the user string/list/tuple.Define a function with a custom name as you like and invoke it by passing the iterator.Initialize the count to 0.Run a loop until it reaches the end.Increment the count by 1Return the count.Example Live Demo## function to calculate lenght of the iterator def length(iterator):    ## initializing the count to 0    count = 0    ## iterating through the iterator    for item in iterator:       ## incrementing count       ... Read More

Why importing star is a bad idea in python

Hafeezul Kareem
Updated on 29-Jun-2020 14:05:12

466 Views

Importing all methods from a module in Python is a bad idea because of the following reasons.It is difficult to find a parent module of the method which we used in the programs.We are not allowed to create our functions with the names of methods.Let's see an example. Below we write a function called add in the sample.py.## sample.py file def add(a, b): return a + bExampleSave the above file in the same directory as below Python file.## let's assume we have module called sample from sample import * def add(*nums):    return sum(nums) print(add(1, 2, 3, 4, ... Read More

Ways to print escape characters in python

Hafeezul Kareem
Updated on 30-Jul-2019 22:30:26

3K+ Views

In this article, we are going to see how we can print the escape characters in Python. I think you know what escape characters are? Let's see what escape characters for those who don't know are?Escape characters are used for the individual meanings in strings. If we want to include a new line, tab space, etc., in strings, we can use these escape characters. Let's see some examples.Example Live Demo## new line new_line_string = "HiHow are you?" ## it will print 'Hi' in first line and 'How are you?' in next line print(new_line_string)OutputIf you run the above program, it will ... Read More

Ways to increment a character in python

Hafeezul Kareem
Updated on 29-Jun-2020 13:48:16

9K+ Views

In this tutorial, we are going to see different methods to increment a character in Python.TypecastingLet's first see what happens if we add an int to char without typecasting.Example Live Demo## str initialization char = "t" ## try to add 1 to char char += 1 ## gets an errorIf you execute above program, it produces the following result −TypeError          Traceback (most recent call last) in ()       3     4 ## try to add 1 to char ----> 5 char += 1 ## gets an error TypeError: must be str, not intTo ... Read More

Can generator be used to create iterators in Python?

Sri
Sri
Updated on 30-Jul-2019 22:30:26

240 Views

Yes, We can create a generator by using iterators in python Creating iterators is easy, we can create a generator by using the keyword yield statement. Python generators are an easy and simple way of creating iterators. and is mainly used to declare a function that behaves like an iterator.The generator is a function which we can iterate over one value at a time most probably in a day to day life, every programmer will use iterable objects like lists, strings, and Dict, etc. An iterator is an object that can be iterated through looping.The following example shows that Generators introduce Yield ... Read More

What does immutable mean? Which Python types are mutable and which are not?

Sri
Sri
Updated on 30-Jul-2019 22:30:26

341 Views

In Python, there are two types of Objects.Mutable ObjectImmutable ObjectMutable: Mutable objects are modified, (i.e) objects are a changeable list, set, dict, etc are mutable.mutable objects are easy to change.Example 1list =["Tutorials ", "Point", "Pvt", "Ltd"] list[2]= 'Tutorix' listOutput['Tutorials ', 'Point', 'Tutorix', 'Ltd'] Example 2list=['Car', 'Bike', 'Scooty', 'Bus', 'Metro'] list[4]= 'Bicycle' listOutput['Car', 'Bike', 'Scooty', 'Bus', 'Bicycle'] Immutable: immutable objects are not modified (i.e) not changeable int, float, bool, str, tuple, Unicode, etc ... are immutable. immutable objects are expensive and difficult to change. a tuple is enclosed within the parenthesis tuples are immutable and can't be changed.Example 1tuple=('1', '2', 'Python', ... Read More

How to Create and Assign Lists in Python?

Sri
Sri
Updated on 30-Jul-2019 22:30:26

2K+ Views

We can Create and Assign List by Insert, Append Length, Index, Remove and Extend, etc.. Lists are mutable and the changeble object is enclosed within the square brackets i.e [ ], Lists in python is easy.Examplelist =["Tutorials ", "Point", "Pvt", "Ltd"] listOutput['Tutorials ', 'Point', 'Pvt', 'Ltd'] Assign Values in Lists To assign values in lists, use square bracketsExamplelist1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print ("list1[0]: ", list1[0]) print ("list2[1:5]: ", list2[1:5])When the above code is executed, Following is the preceding outputOutputlist1[0]: physics list2[1:5]: [2, 3, 4, 5]append() and extend() in python append add ... Read More

What are the tools that help to find bugs or perform static analysis in Python?

Sri
Sri
Updated on 30-Jul-2019 22:30:26

1K+ Views

Pychecker and Pylint are the static analysis tools that help to find bugs in python.Pychecker is an opensource tool for static analysis that detects the bugs from source code and warns about the style and complexity of the bug.Pylint is highly configurable and it acts like special programs to control warnings and errors, it is an extensive configuration file Pylint is also an opensource tool for static code analysis it looks for programming errors and is used for coding standard. it checks the length of each programming line. it checks the variable names according to the project style. it can also be used as ... Read More

What is lambda binding in Python?

Sri
Sri
Updated on 30-Jul-2019 22:30:26

491 Views

When a program or function statement is executed, the current values of formal parameters are saved (on the stack) and within the scope of the statement, they are bound to the values of the actual arguments made in the call. When the statement is exited, the original values of those formal arguments are restored. This protocol is fully recursive. If within the body of a statement, something is done that causes the formal parameters to be bound again, to new values, the lambda-binding scheme guarantees that this will all happen in an orderly manner.there is only one binding for x: ... Read More

Is Python Object Oriented or Procedural?

Sri
Sri
Updated on 30-Jul-2019 22:30:26

3K+ Views

Yes, Python support both Object  Oriented and Procedural  Programming language as it is a high level programming language designed for general purpose programming. Python are multi-paradigm, you can write programs or libraries that are largely procedural, object-oriented, or functional in all of these languages. It depends on what you mean by functional. Python does have some features of a functional language. OOP's concepts like, Classes, Encapsulation, Polymorphism, Inheritance etc.. in Python makes it as a object oriented programming language. In Similar way we can created procedural program through python using loops ,for ,while etc ..and control structure.Exampleclass Rectangle:    def __init__(self, length, breadth, ... Read More

Advertisements