• Slider Banner

    The Programming World

    Learn about top programming languages, tools and frameworks.

    Read more
  • Slider Banner

    Certification MCQ

    Practice for certification from 1000+ multiple choice questions.

    Read more
  • Slider Banner

    Prepare for Technical Interview

    Over 1 Million+ interview questions for different technical profiles.

    Read more

Create a list of the first letters of every word in the string using List Comprehension in Python.

As list comprehensions returns lists, they consist of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element.

mystring = "This is programs Buzz" 
mylist = [word[0] for word in mystring.split()]
print(mylist)

Output: 

['T', 'i', 'p', 'B']

Print "even" or "odd" if the length of a particular word in given sentence is odd or even.

 

mystring = "Welcome to Programs Buzz"

for word in mystring.split():
    if len(word)%2 == 0:
        print(word +" <-- has an even length because it has "+str(len(word)) +" chars")
    elif len(word)%2 == 1: 
        print(word +" <-- has an odd length because it has "+str(len(word)) +" chars")

Output

How to create a list of all numbers between 1 and 50 that are divisible by 3 using List comprehension in Python.

List comprehension is an elegant way to define and create list in python. We can create lists just like mathematical statements and in one line only.

mylist = [x for x in range(1, 51) if x % 3 == 0]
print(mylist)

This will output [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]

How to print all the even numbers from 0 to 10 using range() in Python.

The range() type returns an immutable sequence of numbers between the given start integer to the stop integer. range() constructor has two forms of definition:

range(stop)
range(start, stop[, step])

This is how we can print all even numbers from 0 to 10 using range() in python.

mylist = list(range(0,11,2))
print(mylist)

This will output [0, 2, 4, 6, 8, 10].

Use for, split() method, and if to create a Statement that will print out words that start with 's' using Python.

The split() method splits a string into a list. You can specify the separator, default separator is any whitespace.

mystring = 'Hello, you need to print words which starts with s only in this sentense.'

for word in mystring.split():
    if word[0] == 's':
        print(word)

output:

starts
s
sentense.

In Python what are iterators?

An iterator is an object that contains a countable number of values. An iterator is an object that can be iterated upon, meaning that you can traverse through all the values. Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods __iter__() and __next__().

Lists, tuples, dictionaries, and sets are all iterable objects. They are iterable containers which you can get an iterator from. All these objects have a iter() method which is used to get an iterator. You can also loop through iterator.

Write a Java program to check if String is Palindrome using StringBuilder.

The StringBuilder in Java represents a mutable sequence of characters. Since the String Class in Java creates and immutable sequence of characters, the StringBuilder class provides an alternate to String Class, as it creates a mutable sequence of characters. 

Write a static method called checkNumber with an int parameter number. The method should not return any value and should print "positive" if the parameter number is > 0. "negative" if the parameter number is < 0. "zero" if the parameter number is equal to 0.

 

package testjava.javatest;

public class PositiveNegativeZero
{
	public static void checkNumber(int number)
	{
		if(number > 0)
			System.out.println("positive");
		else if(number < 0)
			System.out.println("negative");
		else if(number == 0)
			System.out.println("zero");
	}
	
	public static void main(String args[])
	{
		checkNumber(10);
		checkNumber(-10);
		checkNumber(0);
	}
} 

 

Subscribe to