Posts

Featured Post

Python: Built-in Functions vs. For & If Loops – 5 Programs Explained

Image
Python’s built-in functions make coding fast and efficient. But understanding how they work under the hood is crucial to mastering Python. This post shows five Python tasks, each implemented in two ways: Using built-in functions Using for loops and if statements ✅ 1. Sum of a List ✅ Using Built-in Function: numbers = [ 10 , 20 , 30 , 40 ] total = sum (numbers) print ( "Sum:" , total) 🔁 Using For Loop: numbers = [ 10 , 20 , 30 , 40 ] total = 0 for num in numbers: total += num print ( "Sum:" , total) ✅ 2. Find Maximum Value ✅ Using Built-in Function: values = [ 3 , 18 , 7 , 24 , 11 ] maximum = max (values) print ( "Max:" , maximum) 🔁 Using For and If: values = [ 3 , 18 , 7 , 24 , 11 ] maximum = values[ 0 ] for val in values: if val > maximum: maximum = val print ( "Max:" , maximum) ✅ 3. Count Vowels in a String ✅ Using Built-ins: text = "hello world" vowel_count = sum ( 1 for ch in text if ch i...

How to Count Vowels in Input Quickly

Image
Here you'll know how python counts characters in the input. This is an interview question asked recently. How to Count Vowels in Input String Below is the Interview Question Vowels = ('aeiou') You need to check any vowels are present in the input string. Vowels means 'a', 'e', 'i', 'o', 'u'. The below function checks each character of input and compares it with the Vowels. 3 Steps to Count Vowels in Input You can achieve this in three steps. 1. Create a function called 'my_vowel' (the name up to you) 2. Use Set & Intersection Method 3. Run the function 1. Created 'my_vowel' Function #!usr/bin/python #This function finds vowel counts #welcome to my function """ Optional description """ def my_vowel (a): vowels = set ( 'aeiou' ) a_1 = vowels . intersection( set (a)) # initialized the dictionary found = { 'a' : 0 , 'e' : 0 , 'i' : 0 ,...

Python - How to Lookup Dictionary by Key

Image
Here's Python Dictionary that explained how to lookup it using Key. Dictionary in Python is Key/Value pair. It's different from the list. The basic rule to identify; is enclosed in flower brackets ({}). Here's a demo about lookup and how to test it.  Dictionary = { 'key' : 'value', 'key: value'  }   IN THIS PAGE Python Dictionary Python Lookup How to check Lookup working or not Dictionary Example my_dict = {'name' : 'srini' , 'salary' : '100000', 'skills' : 'python' } Here, 'name' is the label. Then, : Then, 'srini' -> Value Explanation Data is enclosed in flower brackets It's an unordered list You can manipulate data (mutable) You can access the value of a particular key. So, in Python, it's called a Lookup. It's one of the best interview questions. You May Also Like :  Python Dictionary Vs List With Examples Lookup Dictionary by Key Python Lookup (a.k.a Dictionary). You ...

How to Use List Function in Python

Image
Here's is an answer to calculate the length of the repeated lists. LIST is a principle data type in Python that holds a list of values. Usually strings. List() Function the Real Usage You can do various operations with Lists. The best examples are REPEAT, APPEND, INSERT, EXTEND, COUNT. Still more you can do. Note: This question is from a test recently held by an HR company. HR firms usually ask to take an online test to filter the job-seekers. IN THIS PAGE 1. Question 2. How to Find Length Interview Question  The '* 2' says times the list is repeated. In this question, they have given '2' times. my_list = ['srini', 'seetha', 'rao'] b = len(list(list(list(list(list(my_list*2)))))) Print (b) Select Answer 1. 5 2. 6 3. 7 4. None Explanation: In the first step, the '*' says, you repeated the List two times. It confuses the test-taker since they have given multiple functions of the list(). How to get Length You can code the way I did. So...

How to Find Max and Min Quickly in Python List

Image
Here's logic to find Max and Min values in a List. Without using built-in Max() and Min () functions you can find Max and Min values in a List. For that matter, you need to write a user-defined function. This post is all about how to write it and run. Finding Max and Min in List You can achieve this by writing a user-defined function. Here's useful logic and steps to write it precisely. IN THIS PAGE Write a Function Execute Function Get the result Write Function You can write an user-defined function to get MAX and MIN values. In this function, I am using the max(), min() built-in functions. The other variables you can use as you wish. Below is the my actual logic. Sample Code def findmaxmin (data): ax = max (data) by = min (data) return (x,y) data = ( 109 , 98 , 88 , 7 ) (maximum, minimum) = findmaxmin (data) print ( "Maximum Marks = " , maximum) print ( "Minimum Marks = " , minimum) Created a Script Using vim editor Here as the first step, I ...

How to Write Class Object in Ubuntu Python

Image
Python supports object-oriented programming, which makes python powerful. Creating class in Ubuntu python explained. You can use it in different ways by assigning it to an object. Doing all these are explained in the below steps.   Writing Class in Python in 3 Steps Python code Write the code in script Execute script Writing Class in Python Below sample code my give indentation errors. However, I have corrected the code in the actual script. class Employee: """Base class""" empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 # can also be written as Employee.empCount = Employee.empCount + 1 def displayEmployee(self): # function is defined here print "Name : ", self.name, ", Salary: ", self.salary # "emp1 is the first object of Employee class" emp1 = Employee("Akhil", 2000) # "emp2 is the second object of Employee class" emp2 = Employee(...

How to Read Kafka Logs Quickly

Image
In Kafka, the log file's function is to store entries. Here, you can find entries for the producer's incoming messages. You can call these topics. And,  topics are divided into partitions.

How to Check Kafka Available Brokers

Image
Here's the command to check the list of brokers present in a Kafka Cluster. You can say the Broker is the heart of the Kafka cluster. In simple terms, it works as a process. The main function is to receive messages from publishers and gives permission to access messages by consumers. How to Check Available Brokers in Kafka Here is the command: linux$  ./zookeeper-shell.sh zookeeper-IPaddress:2181 | "ls /brokers/ids" Just use the above command to get the number of brokers present in your host (Kafka Cluster). What is Default Broker-id in Kafka  The default broker id is -1 . When you create a new broker, it adds to -1. Then, it gives a new broker id. The broker ids will be generated from  reserved.broker.max.id + 1. Types of broker ids. Use assigned Zookeeper generated broker id. According to Wiki: Kafka runs on a cluster of one or more servers (called brokers), and the partitions of all topics are distributed across the cluster nodes.  Additionally, partitions are rep...