Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Full_Python_Functions_Cheat_Sheet-1

This document is a comprehensive cheat sheet for Python functions, categorizing built-in functions, string functions, list functions, tuple functions, dictionary functions, file handling functions, math functions, random functions, and OS module functions. Each function is accompanied by a brief description and an example of its usage. It serves as a quick reference guide for Python programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Full_Python_Functions_Cheat_Sheet-1

This document is a comprehensive cheat sheet for Python functions, categorizing built-in functions, string functions, list functions, tuple functions, dictionary functions, file handling functions, math functions, random functions, and OS module functions. Each function is accompanied by a brief description and an example of its usage. It serves as a quick reference guide for Python programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Complete Python Functions Cheat Sheet

### Built-in Functions


1. print() - Displays output on the screen.
Example: print("Hello, World!") -> Hello, World!

2. input() - Takes user input as a string.


Example: name = input("Enter your name: ")

3. len() - Returns the length of an object.


Example: len("Python") -> 6

4. type() - Returns the type of a variable.


Example: type(5.0) -> <class 'float'>

5. id() - Returns the memory address of an object.


Example: id(10)

6. isinstance() - Checks if an object is an instance of a specific class.


Example: isinstance(5, int) -> True

7. range() - Generates a sequence of numbers.


Example: list(range(1, 5)) -> [1, 2, 3, 4]

8. abs() - Returns the absolute value of a number.


Example: abs(-10) -> 10

9. round() - Rounds a number to a specified decimal places.


Example: round(3.456, 2) -> 3.46

10. pow() - Returns x raised to the power y.


Example: pow(2, 3) -> 8

### String Functions


1. upper() - Converts a string to uppercase.
Example: "hello".upper() -> "HELLO"
2. lower() - Converts a string to lowercase.
Example: "HELLO".lower() -> "hello"

3. capitalize() - Capitalizes the first letter.


Example: "python".capitalize() -> "Python"

4. title() - Capitalizes the first letter of each word.


Example: "hello world".title() -> "Hello World"

5. strip() - Removes leading and trailing spaces.


Example: " hello ".strip() -> "hello"

6. replace() - Replaces part of a string.


Example: "hello".replace("h", "y") -> "yello"

7. split() - Splits a string into a list.


Example: "a,b,c".split(",") -> ['a', 'b', 'c']

8. join() - Joins a list into a string.


Example: "-".join(["a", "b", "c"]) -> "a-b-c"

### List Functions


1. append() - Adds an item to the end.
Example: lst = [1, 2]; lst.append(3)

2. extend() - Extends a list with another iterable.


Example: [1, 2].extend([3, 4]) -> [1, 2, 3, 4]

3. insert() - Inserts an item at a given index.


Example: lst = [1, 3]; lst.insert(1, 2)

4. remove() - Removes the first occurrence of a value.


Example: [1, 2, 3].remove(2)

5. pop() - Removes and returns an item.


Example: [1, 2, 3].pop(1) -> 2
### Tuple Functions
1. count() - Returns the number of occurrences of a value.
Example: (1, 2, 2, 3).count(2) -> 2

2. index() - Returns the index of the first occurrence of a value.


Example: (1, 2, 3).index(2) -> 1

### Dictionary Functions


1. keys() - Returns a view of all keys.
Example: {"a": 1, "b": 2}.keys() -> dict_keys(['a', 'b'])

2. values() - Returns a view of all values.


Example: {"a": 1, "b": 2}.values() -> dict_values([1, 2])

3. items() - Returns key-value pairs as tuples.


Example: {"a": 1, "b": 2}.items() -> dict_items([('a', 1), ('b', 2)])

4. get() - Retrieves a value by key (returns None if key is not found).


Example: {"a": 1}.get("a") -> 1

### File Handling Functions


1. open() - Opens a file.
Example: file = open("test.txt", "r")

2. read() - Reads the contents of a file.


Example: file.read()

3. write() - Writes to a file.


Example: file.write("Hello")

4. close() - Closes the file.


Example: file.close()

### Math Functions


1. sqrt() - Returns the square root.
Example: import math; math.sqrt(16) -> 4.0

2. ceil() - Rounds a number up.


Example: math.ceil(3.2) -> 4

3. floor() - Rounds a number down.


Example: math.floor(3.7) -> 3

4. factorial() - Returns the factorial.


Example: math.factorial(5) -> 120

### Random Functions


1. random() - Returns a random float between 0 and 1.
Example: import random; random.random()

2. randint() - Returns a random integer within a range.


Example: random.randint(1, 10)

3. choice() - Returns a random element from a sequence.


Example: random.choice(["a", "b", "c"])

### OS Module Functions


1. getcwd() - Returns the current working directory.
Example: import os; os.getcwd()

2. chdir() - Changes the current directory.


Example: os.chdir("/path/to/dir")

3. listdir() - Lists all files in a directory.


Example: os.listdir(".")

4. mkdir() - Creates a new directory.


Example: os.mkdir("new_folder")

5. remove() - Deletes a file.


Example: os.remove("file.txt")

You might also like