Aug 27, 2018

C++ Basics and Python

Python

dictionary get() Method

The method get() returns a value for the given key. If key is not available then returns default value None.

dict.get(key, default = None)
  • key − This is the Key to be searched in the dictionary.

  • default − This is the Value to be returned in case key does not exist.

dict = {'Name': 'Zabra', 'Age': 7}
print "Value : %s" %  dict.get('Age')
print "Value : %s" %  dict.get('Education', "Never")
Output:

Value : 7
Value : Never

C++

abs()

Returns the absolute value of parameter n .

std::tolower(charT c, const locale& loc)

Converts parameter c to its lowercase equivalent if c is an uppercase letter and has a lowercase equivalent, as determined by the ctype facet of locale loc. If no such conversion is possible, the value returned is c unchanged.

std::string::erase()

Erases part of the string, reducing its length

  1. sequence: Erases the portion of the string value that begins at the character position pos and spans len characters (or until the end of the string, if either the content is too short or if len is string::npos. Notice that the default argument erases all characters in the string (like member function clear).

  2. character: Erases the character pointed by p.

  3. range: Erases the sequence of characters in the range [first,last).

std::string::substr()

Returns a newly constructed string object with its value initialized to a copy of a substring of this object. The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).

  • pos

    • Position of the first character to be copied as a substring. If this is equal to the string length, the function returns an empty string. If this is greater than the string length, it throws out_of_range. Note: The first character is denoted by a value of 0 (not 1).

  • len

    • Number of characters to include in the substring (if the string is shorter, as many characters as possible are used). A value of string::npos indicates all characters until the end of the string.

Last updated