Skip to main content

Chapter 2 - Python

 

LIST

In Python, a list is a built-in data type that represents an ordered sequence of elements. Lists are versatile and can contain elements of different data types, including numbers, strings, and even other lists. Here are some basic operations you can perform on lists:

1. Creating a List:

python
my_list = [1, 2, 3, "apple", "banana"]

2. Accessing Elements:

You can access elements in a list using their indices. Indices start from 0.

python
first_element = my_list[0] # Access the first element second_element = my_list[1] # Access the second element last_element = my_list[-1] # Access the last element

3. Slicing:

You can extract a subset of a list using slicing.

python
subset = my_list[1:4] # Extract elements from index 1 to 3 (excluding 4)

4. Appending Elements:

You can add elements to the end of a list using the append() method.

python
my_list.append("orange")

5. Inserting Elements:

You can insert an element at a specific position using the insert() method.

python
my_list.insert(1, "grape") # Insert "grape" at index 1

6. Removing Elements:

You can remove elements by value using the remove() method or by index using the pop() method.

python
my_list.remove("banana") # Remove the element with the value "banana" popped_element = my_list.pop(2) # Remove and return the element at index 2

7. Updating Elements:

You can update the value of an element by assigning a new value.

python
my_list[0] = 100 # Update the value at index 0 to 100

8. Length of a List:

You can get the length of a list using the len() function.

python
length = len(my_list)

9. Concatenating Lists:

You can concatenate two lists using the + operator.

python
new_list = my_list + [5, 6, 7]

10. Checking Membership:

You can check if an element is present in a list using the in keyword.

python
is_present = "apple" in my_list

11. Iterating Over a List:

You can iterate over the elements of a list using a for loop.

python
for item in my_list: print(item)

These are some of the basic operations you can perform on lists in Python. Lists are widely used for storing and manipulating data in various programming scenarios.




Dictionary


In Python, a dictionary is a built-in data type that represents an unordered collection of key-value pairs. Each key in a dictionary must be unique, and it is associated with a specific value. Dictionaries are defined by curly braces {}. Here are some basic operations you can perform on dictionaries:

1. Creating a Dictionary:

python
my_dict = {"name": "John", "age": 25, "city": "New York"}

2. Accessing Values:

You can access the value associated with a key using square brackets [].

python
name = my_dict["name"] age = my_dict["age"]

3. Updating Values:

You can update the value associated with a key.

python
my_dict["age"] = 26

4. Adding New Key-Value Pairs:

You can add new key-value pairs to a dictionary.

python
my_dict["occupation"] = "Engineer"

5. Removing Key-Value Pairs:

You can remove a key-value pair using the del keyword or the pop() method.

python
del my_dict["city"] # Remove the key-value pair with key "city" removed_value = my_dict.pop("age") # Remove and return the value for the key "age"

6. Checking Membership:

You can check if a key is present in a dictionary using the in keyword.

python
is_present = "age" in my_dict

7. Getting Keys and Values:

You can get a list of all keys or values using the keys() and values() methods, respectively.

python
all_keys = my_dict.keys() all_values = my_dict.values()

8. Getting Key-Value Pairs:

You can get a list of all key-value pairs using the items() method.

python
all_items = my_dict.items()

9. Iterating Over a Dictionary:

You can iterate over the keys, values, or items of a dictionary using a for loop.

python
# Iterate over keys for key in my_dict: print(key) # Iterate over values for value in my_dict.values(): print(value) # Iterate over key-value pairs for key, value in my_dict.items(): print(key, value)

10. Length of a Dictionary:

You can get the number of key-value pairs in a dictionary using the len() function.

python
length = len(my_dict)

Dictionaries are commonly used for representing structured data and are suitable for scenarios where you need to quickly look up values based on unique keys.

Comments

Popular posts from this blog

Lending Cycle in USA. With details on Freddie Mac and Fannie Mae

KEY TERMS Mortgage  A  mortgage loan  is a loan used  to raise funds to buy real estate, or by existing property owners to raise funds for any purpose while putting a  lien  on the property being mortgaged. The loan is " secured " on the borrower's property; t his means that a  legal mechanism  is put into place which allows the lender to take possession and sell the secured property (" foreclosure " or " repossession ") to pay off the loan in the event the borrower defaults on the loan or otherwise fails to abide by its terms. Fungible Fungibility implies that two things are identical in specification, where individual units can be mutually substituted.  Commodities, common shares, options, and dollar bills are examples of fungible goods. Security Refers to a fungible financial instrument.   A security can represent ownership in a corporation in the form of stock, a creditor relationship with a governmental body or a corporation re...

Stock Analysis - Fundamentals

  PB <= 1 A company's price-to-book ratio is the company's current stock price per share divided by its book value per share (BVPS). Dividend Yield (More the better) The dividend yield is a financial ratio that shows how much a company pays out in dividends each year relative to its stock price. PEG Ratio : Lower the better The price/earnings to growth ratio (PEG ratio) is a stock's price-to-earnings (P/E) ratio divided by the growth rate of its earnings Current Ratio: More the better The current ratio, also known as the working capital ratio, measures the capability of a business to meet its short-term obligations that are due within a year. Current Assets / Current Liabilities Quick Ratio: More the better The quick ratio measures the dollar amount of liquid assets available against the dollar amount of current liabilities of a company. The quick ratio provides a more stringent measure of liquidity than the current ratio because it excludes inventory, which ma...

Retirement Corpus - The FIRE Approach

THE FIRE APPROACH FOR RETIREMENT CORPUS Reference Excel Excel Used for the Demonstration