Posts

Strings in Python

  Count the number of characters in a given string Program: text="hello how are you?" print(len(text)) Output:  18 Count the number of words in a given string Program: #split function is used to splits a string into list. text="hello how are you?" print(len(text.split()))  Output:   4 Print Unique words from the given string Program : text="hello how are you where are you are you going threre" print(set(text.split())) Output: {'you', 'where', 'are', 'threre', 'how', 'hello', 'going'}   Print Unique words from the given string Program : text="hello how are you where are you are you going threre" print(len(set(text.split())))     Output: 7 Calculation of Lexical Diversity One of the most commonly used approaches to measure LD is to use the ratio of unique lexical items divided by the total number of words in a sample   Program text="hello how are you where are you are you going threre...

Dictionaries in Python

Image
  Dictionaries- A dictionary is a group of elements arranged in the form of key-value pairs. In the dictionary the first element is considered as key and the next element is considered as value. The key and value are separated by colon(:), Syntax: dict1={key1:value1,key2:value2,key3:value3,…..} Ex:- dict1={“bname”:”c”,”aname”:”dennisritche”,”pyear”:1976} Here dict1 is name of the dictionary, bname,aname,pyear are key elements and “c”,”dennisritche”, 1976 are values to the key elements respectively. Operations on dictionaries:- >>> dict1={"bname":"C","aname":"Dennis","pyear":1976} >>> dict1 {'bname': 'C', 'aname': 'Dennis', 'pyear': 1976} >>> dict1["bname"] 'C' >>> dict1["aname"] 'Dennis' len(dictionaryname)- returns the number of elements in the dictionary. >>> n=len(dict1) >>...

Lists in Python

Image
  Lists: A list is similar to an array that consists of a group of elements or items. Just like an array, a list can store elements. But, the main difference between an array and list is, an array can store only similar type of elements whereas a list can store dissimilar type of elements. Lists are mutable which means we can modify the list after its creation. student_info=[101,”Bharathi”,”kbncollege”, 78.5,89,99] l=[1,2,3,4,5] print(student_info) lists can also be created using range() function. l=list(range(1,10,2)) print(l) >>> l=[1,2,3,4,5] >>> type(l) <class 'list'> >>> l [1, 2, 3, 4, 5] >>> print(l) [1, 2, 3, 4, 5] >>> l1=list(range(1,10,2)) >>> l1 [1, 3, 5, 7, 9]   How to insert an element into a lists 1.   append(item)- an item is placed at the end of the list. >>> l1=[101,"bharathi",56.7,88,99,123] >>> l1.append(200) >>> l1 ...