Found 10784 Articles for Python

Python program to find occurrence to each character in given string

Pavitra
Updated on 11-Jul-2020 11:31:49

2K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a string, we need to find the occurrence of each character in a given string.Here we will be discussing 3 approaches as discussed below:LApproach 1 − The brute-force approachExample Live Demotest_str = "Tutorialspoint" #count dictionary count_dict = {} for i in test_str:    #for existing characters in the dictionary    if i in count_dict:       count_dict[i] += 1    #for new characters to be added    else:       count_dict[i] = 1 print ("Count of all characters in Tutorialspoint ... Read More

Python program to find the largest number in a list

Pavitra
Updated on 23-Dec-2019 08:09:45

936 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to calculate the largest element of the list.Here we will take the help of built-in functions to reach the solution of the problem statementUsing sort() functionExample# list list1 = [23, 1, 32, 67, 2, 34, 12] # sorting list1.sort() # printing the last element print("Largest element is:", list1[-1])OutputLargest in given array is 67Using max() functionExample Live Demo# list list1 = [23, 1, 32, 67, 2, 34, 12] # printing the maximum element print("Largest element is:", max(list1))OutputLargest in given ... Read More

Python Program to find the largest element in an array

Pavitra
Updated on 23-Dec-2019 08:07:18

1K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to calculate the largest element of the array.Here we use the bruteforce approach in which we compute the largest element by traversing the whole loop and get the element.We can observe the implementation below.Example Live Demo# largest function def largest(arr, n):    #maximum element    max = arr[0]    # traverse the whole loop    for i in range(1, n):       if arr[i] > max:          max = arr[i]    return max # ... Read More

Python program to create a dictionary from a string

Pavitra
Updated on 11-Jul-2020 11:32:35

554 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a string input, we need to convert it into dictionary typeHere we will discuss two methods to solve the problem without using a built-in dict() function.Method 1 − Using eval() methodEval method is used only when the syntax or the formation of string resembles that of a dictionary. Direct conversion of string to the dictionary can happen in that case as discussed below.Example Live Demo# String string = "{'T':1, 'U':2, 'T':3, 'O':4, 'R':5}" # eval() function dict_string = eval(string) print(dict_string) print(dict_string['T']) print(dict_string['T'])Output{'T': ... Read More

Count words in a sentence in Python program

Pavitra
Updated on 25-Aug-2023 02:03:41

33K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a string we need to count the number of words in the stringApproach 1 − Using split() functionThe split() function breaks the string into a list iterable with space as a delimiter. if the split() function is used without specifying the delimiter space is allocated as a default delimiter.Example Live Demotest_string = "Tutorials point is a learning platform" #original string print ("The original string is : " + test_string) # using split() function res = len(test_string.split()) # total no of words print ... Read More

Count upper and lower case characters without using inbuilt functions in Python program

Pavitra
Updated on 23-Dec-2019 07:54:17

516 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a string, we need to count the number of uppercase and lowercase characters present in the string without using the inbuilt functionThis can be easily solved by using islower() and isupper() function available in python. But here there is a constraint to use the inbuilt function. So here we take the help of the ASCII value of the characters.Using the ord() function we compute the ASCII value of each character present in the string and then compare to check for uppercase ... Read More

Count positive and negative numbers in a list in Python program

Pavitra
Updated on 11-Jul-2020 11:24:45

3K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list iterable, we need to count positive and negative numbers in it and display them.Approach 1 − Brute-force approach using iteration construct(for)Here we need to iterate each element in the list using a for loop and check whether num>=0, to filter the positive numbers. If the condition evaluates to be true, then increase pos_count otherwise, increase neg_count.Example Live Demolist1 = [1, -2, -4, 6, 7, -23, 45, -0] pos_count, neg_count = 0, 0 # enhanced for loop   for num in ... Read More

Python program to Count Even and Odd numbers in a List

Pavitra
Updated on 11-Jul-2020 11:25:39

5K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to count even and odd numbers in a list.There are three methods as discussed below−Approach 1 − Using brute-force approachExample Live Demolist1 = [21, 3, 4, 6, 33, 2, 3, 1, 3, 76] even_count, odd_count = 0, 0 # enhanced for loop for num in list1:    #even numbers    if num % 2 == 0:       even_count += 1    #odd numbers    else:       odd_count += 1 print("Even numbers available in the ... Read More

Python Program to convert Kilometers to Miles

Pavitra
Updated on 23-Dec-2019 07:37:08

227 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given distance in kilometers and we need to convert it into milesAs we know that 1 kilometer equals 0.62137 miles.Formula UsedMiles = kilometer * 0.62137Now let’s observe the concept in the implementation below−Example Live Demokilometers = 5.5 # conversion factor as 1 km = 0.621371 miles conv = 0.621371 # calculation miles = kilometers * conv print(kilometers, "kilometers is equal to ", miles, "miles")Output5.5 kilometers is equal to 3.4175405 milesAll the variables are declared in the local scope and their references are seen ... Read More

Python program to convert hex string to decimal

Pavitra
Updated on 23-Dec-2019 07:35:02

366 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a hexadecimal string, we need to convert it into its decimal equivalent.We have two approaches to solve the problem−Brute-force ApproachUsing Built-in moduleBrute-Force MethodHere we take the help of explicit type casting function i.e. integer. This function takes two arguments i.e. hex equivalent and base i.e. (16). This function is used to convert a hexadecimal string to its equivalent decimal in an integer type, which can be further typecasted back to string format.Example Live Demo#input string string = 'F' # converting hexadecimal string ... Read More

Advertisements