DEV Community

Sandeep
Sandeep

Posted on

Python Cheat Sheet Part - 2

Strings
Python string is a sequence of characters, and each character can be individually accessed using its index.

String
You can create Strings by enclosing text in both forms of quotes - single quotes or double quotes.

variable_name = "String Data"
Enter fullscreen mode Exit fullscreen mode

Slicing
Slicing refers to obtaining a sub-string from the given string. The following code will include index 1, 2, 3, and 4 for the variable named var_name

var_name[1 : 5]
Enter fullscreen mode Exit fullscreen mode

isalnum() method
Returns True if all the characters in the string are alphanumeric, else False

string_variable.isalnum()
Enter fullscreen mode Exit fullscreen mode

isalpha() method
Returns True if all the characters in the string are alphabets

string_variable.isalpha()
Enter fullscreen mode Exit fullscreen mode

isdecimal() method
Returns True if all the characters in the string are decimals

string_variable.isdecimal()
Enter fullscreen mode Exit fullscreen mode

isdigit() method
Returns True if all the characters in the string are digits

string_variable.isdigit()
Enter fullscreen mode Exit fullscreen mode

islower() method
Returns True if all characters in the string are lower case

string_variable.islower()
Enter fullscreen mode Exit fullscreen mode

isspace() method
Returns True if all characters in the string are whitespaces

string_variable.isspace()
Enter fullscreen mode Exit fullscreen mode

isupper() method
Returns True if all characters in the string are upper case

string_variable.isupper()
Enter fullscreen mode Exit fullscreen mode

lower() method
Converts a string into lower case equivalent

string_variable.lower()
Enter fullscreen mode Exit fullscreen mode

upper() method
Converts a string into upper case equivalent

string_variable.upper()
Enter fullscreen mode Exit fullscreen mode

strip() method
It removes leading and trailing spaces in the string

string_variable.strip()

Enter fullscreen mode Exit fullscreen mode

List
A List in Python represents a list of comma-separated values of any data type between square brackets.

var_name = [element1, element2, ...]
Enter fullscreen mode Exit fullscreen mode

index method
Returns the index of the first element with the specified value

list.index(element)
Enter fullscreen mode Exit fullscreen mode

append method
Adds an element at the end of the list

list.append(element)
Enter fullscreen mode Exit fullscreen mode

extend method
Add the elements of a given list (or any iterable) to the end of the current list

list.extend(iterable)
Enter fullscreen mode Exit fullscreen mode

insert method
Adds an element at the specified position

list.insert(position, element)
Enter fullscreen mode Exit fullscreen mode

pop method
Removes the element at the specified position and returns it

list.pop(position)
Enter fullscreen mode Exit fullscreen mode

remove method
The remove() method removes the first occurrence of a given item from the list

list.remove(element)
Enter fullscreen mode Exit fullscreen mode

clear method
Removes all the elements from the list

list.clear()
Enter fullscreen mode Exit fullscreen mode

count method
Returns the number of elements with the specified value

list.count(value)
Enter fullscreen mode Exit fullscreen mode

reverse method
Reverses the order of the list

list.reverse()
Enter fullscreen mode Exit fullscreen mode

sort method
Sorts the list

list.sort(reverse=True|False)
Enter fullscreen mode Exit fullscreen mode

Tuples
Tuples are represented as comma-separated values of any data type within parentheses.

Tuple Creation

variable_name = (element1, element2, ...)
Enter fullscreen mode Exit fullscreen mode

Lets talk about some of the tuple methods:

count method
It returns the number of times a specified value occurs in a tuple

tuple.count(value)
Enter fullscreen mode Exit fullscreen mode

index method
It searches the tuple for a specified value and returns the position.

tuple.index(value)
Enter fullscreen mode Exit fullscreen mode

Sets
A set is a collection of multiple values which is both unordered and unindexed. It is written in curly brackets.

Set Creation: Way 1

var_name = {element1, element2, ...}
Enter fullscreen mode Exit fullscreen mode

Set Creation: Way 2

var_name = set([element1, element2, ...])
Enter fullscreen mode Exit fullscreen mode

Set Methods
Lets talk about some of the methods of sets:

add() method
Adds an element to a set

set.add(element)
Enter fullscreen mode Exit fullscreen mode

clear() method
Remove all elements from a set

set.clear()
Enter fullscreen mode Exit fullscreen mode

discard() method
Removes the specified item from the set

set.discard(value)
Enter fullscreen mode Exit fullscreen mode

intersection() method
Returns intersection of two or more sets

set.intersection(set1, set2 ... etc)
Enter fullscreen mode Exit fullscreen mode

issubset() method
Checks if a set is a subset of another set

set.issubset(set)
Enter fullscreen mode Exit fullscreen mode

pop() method
Removes an element from the set

set.pop()
Enter fullscreen mode Exit fullscreen mode

remove() method
Removes the specified element from the set

set.remove(item)
Enter fullscreen mode Exit fullscreen mode

union() method
Returns the union of two or more sets

set.union(set1, set2...)
Enter fullscreen mode Exit fullscreen mode

Python cheat sheet part-1

Top Websites to learn Python

How to install python in windows 10

Best IDE's for Python

Top comments (0)