Python Notes
Module 1
- type (variable) -> gives the type like int, boolean, string etc
- Print/ format/ {} -> # You can place two or more placeholders "{}" with print() function
Note that order is important
year = 2022
revenue = 394 print("Apple annual revenue in {} was ${}B".format(year, revenue))
here $ will come b4 revenue and B(billion) after revenue
- input function -> input data from user
type casting -> input = float(print("enter amt"))
Using the input() function, the value entered by the user will be stored in a string data type by
default.
Module 2
List
list indexing starts from 0
List Slicing -> to access more than 1 element of list
use ":"
mycompanies[1:4]
To find last element of list :
- List[-1]
- print([Link]())
Python Dictionary
#Let's add a new item to an existing dictionary
investor_assets["Alternative Investments"] = 4000
print(investor_assets)
# Remove an item from the dictionary
del investor_assets["Fixed Income"]
print(investor_assets)
# You access specific dictionary values using the keys
investor_assets["Fixed Income"]
# To find average
sum(technology_stocks.values())/len(technology_stocks)
Strings
# The .upper() method is used to convert the string into uppercase
welcome_message.upper()
# The .split() method is used to divide up the string into words
# The output from the .split() method is a list which is denoted by square
bracket
words = welcome_message.split()
words
# You can combine both strings together and add spaces in between
full_name = first_name + " " + last_name
full_name
Comparison operators
Returns True or False
Conditional Statements (IF-ELSE)
if: -> elif: -> elif: -> else:
round(stock_valuation, 2)
Module 3
"For" Loops
company_revenues = [600000, 900000, 1000000, 1100000]
# Define an accumulator and initialize it to zero
total_revenue = 0
for i in company_revenues:
total_revenue = total_revenue + i
total_revenue
# to find product of list
import math
[Link](my_list)
Range ()