Programming Articles - Page 2562 of 3366

Can generator be used to create iterators in Python?

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

336 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

Program to find how many times a character appears in a string in PHP

Alok Prasad
Updated on 29-Jun-2020 11:59:22

2K+ Views

Example Live DemoOutputw appears 1 times e appears 2 times l appears 2 times c appears 1 times o appears 4 times m appears 1 times t appears 4 times u appears 1 times r appears 1 times i appears 2 times a appears 1 times s appears 1 times p appears 1 times n appears 1 times

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

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

518 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

What is traits in PHP?

Alok Prasad
Updated on 08-Nov-2023 10:43:18

3K+ Views

In 5.4 PHP version trait is introduced to PHP object-oriented programming. A trait is like class however it is only for grouping methods in a fine-grained and reliable way. It isn't permitted to instantiate a trait on its own. Traits are introduced to PHP 5.4 to overcome the problems of single inheritance. As we know in single inheritance class can only inherit from one other single class. In the case of trait, it enables a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. Example Output Result of addition two numbers:8 ... Read More

How to Create and Assign Lists in Python?

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

3K+ 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

Is there any advantage to using __construct() instead of the class's name for a constructor in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:51:56

460 Views

Yes, there are several advantages to using the magic function __construct() instead of the class's name. Those are listed below −The magic function __construct is introduced in PHP 5.4. One advantage of using __construct() over ClassName() as a constructor is if you change the name of the class, you don't need to update the constructor which supports DRY(don't repeat yourself) concept.If you have a child class you can call parent::__construct()to call the parent constructor in an easy way.Example Live DemoOutputThe class "myclass" was initiated! In SubClass constructorNote"__CLASS__" is what’s called a magic constant, which, in this case, returns the name of ... 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

2K+ 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

Can we define a static constructor in Java?

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

5K+ Views

No, we cannot define a static constructor in Java, If we are trying to define a constructor with the static keyword a compile-time error will occur.In general, static means class level. A constructor will be used to assign initial values for the instance variables. Both static and constructor are different and opposite to each other. We need to assign initial values for an instance variable we can use a constructor. We need to assign static variables we can use Static Blocks.ExampleLive Demopublic class StaticConstructorTest {    int x = 10;    // Declaratiopn of Static Constructor    static StaticConstructorTest() { ... Read More

What is dependency injection in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:54:40

12K+ Views

Dependency injection is a procedure where one object supplies the dependencies of another object. Dependency Injection is a software design approach that allows avoiding hard-coding dependencies and makes it possible to change the dependencies both at runtime and compile time.There are numerous approaches to inject objects, here are couple generally known −Constructor InjectionIn this approach, we can inject an object through the class constructor.Example Live DemoOutput3Setter Injectionwhere you inject the object to your class through a setter function.ExampleBenefits of Dependency InjectionAdding a new dependency is as easy as adding a new setter method, which does not interfere with the existing code.Read More

When can we use intern() method of String class in Java?

raja
Updated on 07-Feb-2020 05:45:33

483 Views

The intern() method of String class can be used to deal with the string duplication problems in Java. Using intern() we can save a lot of memory consumed by duplicate string instances. A string is duplicate if it contains the same content as another string but it can be occupied different memory locations.We know that JVM maintains a separate heap memory for string literals for the performance. Once we declare a string literal it will go to this pool and if another variable is assigned with the same literal value, it will be picked from the pool instead of creating a new ... Read More

Advertisements