print(value)
The classic way to show something on screen. Essential for seeing what your code is doing. You can also use f-strings for dynamic output: name = "Naveen"
, then print(f"User: {name}")
.
Here's a quick reference for some of the most common tools in your Python arsenal. Think of it as your personal cyberdeck quick-menu.
These are your basic commands, the bread and butter of any script.
The classic way to show something on screen. Essential for seeing what your code is doing. You can also use f-strings for dynamic output: name = "Naveen"
, then print(f"User: {name}")
.
Gets input from the user. It always returns the data as a string. Example: user_ip = input("Enter target IP: ")
.
Tells you the size of something, like characters in a string or items in a list. len([1, 2, 3])
will give you 3
.
Identifies what kind of data you're dealing with. Running type(10)
tells you it's an <class 'int'>
.
These are your data converters. int("5")
turns text into a number, str(123)
turns a number into text, and list("abc")
turns a string into ['a', 'b', 'c']
.
Text is data. Here's how you slice it, dice it, and put it back together.
Lets you change the case of your text. 'hello'.upper()
gives you 'HELLO'
, and 'HELLO'.lower()
brings it back to 'hello'
.
Cleans up messy text by removing any extra whitespace. .strip()
cleans both ends, .lstrip()
cleans the left, and .rstrip()
cleans the right.
Finds a piece of text and swaps it with something else. For instance, 'a-b-c'.replace('-', ' ')
returns 'a b c'
.
Breaks a string into a list of smaller strings. It's perfect for parsing data, like turning '192.168.1.1'.split('.')
into ['192', '168', '1', '1']
.
The opposite of split. It takes a list of strings and joins them into one. '-'.join(['a', 'b'])
will produce 'a-b'
.
Returns the index (position) of a substring. If it's not found, it returns -1
, which is useful for checks. 'abc'.find('b')
returns 1
.
Lists are your go-to for storing collections of items in order.
Use .append()
to add a single item to the end of a list. If you want to add all the items from another list, use .extend()
.
.pop()
removes an item by its position (index) and gives it back to you. .remove()
hunts down and removes the first item that matches a specific value.
This will sort the list for real, modifying the original. You can sort in reverse with my_list.sort(reverse=True)
.
Lets you grab parts of a list. my_list[1:4]
gets items from index 1 up to (but not including) index 4. my_list[:3]
gets the first three, and my_list[-1]
gets the last item.
Dictionaries store data as key-value pairs, like a high-tech filing cabinet.
You can get a value by its key: my_dict['config']
. You can add or update an entry the same way: my_dict['new_key'] = 'new_value'
.
These methods let you grab all the keys, all the values, or all the key-value pairs from a dictionary, which is great for looping.
A safe way to get a value. If the key you're looking for doesn't exist, it will give you a default value (like None
or whatever you specify) instead of crashing your program. Example: port = config.get('port', 80)
.