0% found this document useful (0 votes)
8 views6 pages

Uday_codes_python_3

The document provides an overview of built-in functions in Python, including their usage for data type conversion, sequence manipulation, and mathematical operations. It also discusses floating point approximation issues, string handling with escape characters, and set and dictionary methods for managing collections of data. Key concepts include unique elements in sets, immutability of set items, and dictionary views for accessing keys, values, and items.

Uploaded by

Vicky Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Uday_codes_python_3

The document provides an overview of built-in functions in Python, including their usage for data type conversion, sequence manipulation, and mathematical operations. It also discusses floating point approximation issues, string handling with escape characters, and set and dictionary methods for managing collections of data. Key concepts include unique elements in sets, immutability of set items, and dictionary views for accessing keys, values, and items.

Uploaded by

Vicky Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Built - In - Functions

Name Usage
print() Function prints the message to the screen or any other standard output device.

int() Converts valid data of any type to integer.

str() Converts data of any type to a string.

id() To find the id of a object.

round(number, Rounds the float value to the given number of decimal digits.
digits(optional))

bool() Converts to boolean data type.

ord(character) Gives unicode value of the character.

chr(unicode) Gives character with the unicode value.

list(sequence) Takes a sequence and converts it into list.

tuple(sequence) Takes a sequence and converts it into tuple.

set(sequence) Takes any sequence as argument and converts to set, avoiding duplicates.

dict(sequence) Takes any number of key-value pairs and converts to dictionary.

float() Converts to float data type.

type() Check the datatype of the variable or value using.

min() Returns the smallest item in a sequence or the smallest of two or more arguments.

max() Returns the largest item in a sequence or the largest of two or more arguments.

sum(sequence) Returns the sum of items in a sequence.

sorted(sequence) Returns a new sequence with all the items in the given sequence ordered in increasing
order.

sorted(sequence, Returns a new sequence with all the items in the given sequence ordered in
reverse=True) decreasing order.

len(sequence) Returns the length of the sequence.

map() Applies a given function to each item of a sequence (list, tuple etc.) and returns a
sequence of the results.

filter() Method filters the given sequence with the help of a function that tests each element
in the sequence to be true or not.

reduce() Receives two arguments, a function and an iterable. However, it doesn't return another
iterable, instead, it returns a single value.

Floating Point Approximation : Float values are stored approximately.


print(0.1 + 0.2) # 0.30000000000000004

Floating Point Errors : Sometimes, floating point approximation gives unexpected results.
print((0.1 + 0.2) == 0.3) # False

Different compound assignment operators are : +=, -=, *=, /=, %=


a = 10
a += 1
print(a) # 11

a = 10
a -= 2
print(a) # 8

a = 10
a /= 2
print(a) # 5.0

a = 10
a %= 2
print(a) # 0

Single And Double Quotes : A string is a sequence of characters enclosed within quotes.
sport = 'Cricket'
sport = "Cricket"

Escape Characters : Escape Characters are a sequence of characters in a string that is interpreted differently by the
computer. We use escape characters to insert characters that are illegal in a string.
print("Hello\nWorld" )

# Output is:
Hello
World

We got a new line by adding \n escape character.

Name Usage
\n New Line

\t Tab Space

\\ Backslash

\' Single Quote

\" Double Quote

Set Methods, Operations and Comparisons

Set Methods :

Name Syntax Usage


add() set.add(value) Adds the item to the set, if the item is not present already.

update() set.update(sequence) Adds multiple items to the set, and duplicates are avoided.

discard() set.discard(value) Takes a single value and removes if present.


No Duplicate Items : Sets contain unique elements
set_a = {"a", "b", "c", "a" }
print(set_a ) # {'b', 'a', 'c'}

Immutable Items : Set items must be immutable. As List is mutable, Set cannot have list as an item.
set_a = {"a", ["c", "a"]}
print(set_a ) # TypeError: unhashable type: 'list'

Dictionary Views & Methods

Dictionary Views :

View Syntax Usage


keys dict.keys() Returns dictionary Keys.

Values dict.values() Returns dictionary Values.

items dict.items() Returns dictionary items(key-value) pairs.

Dictionary Methods :

Name Syntax Usage


copy dict.copy() Returns copy of a dictionary.

update dict.update(iterable) Inserts the specified items to the dictionary.

clear dict.clear() Removes all the elements from a dictionary.

You might also like