Python Tutorial - 1

1 Python Operations
1.1 Variables Assignment
1.2 Math Operations
1.3 Order of Operations (Precedence)
1.4 Print Operations
1.5 Get User Input

2 Data Types
2.1 Booleans
2.2 List
2.3 Dictionary
2.4 Strings
2.5 Tuples
2.6 Sets

3 Python Operators
3.1 Comparison Operators
3.2 Logical Operators
3.3 Conditional Statements


1 Python Operations

Below are the types of operations in python.

1.1 Variable Assignment

# Define a variable named "Number" and assign a number (integer) to it
# Integer is a whole number (no decimals) that could be positive or negative 
Number = 5000;
# Let's view "Number"
Number;
5000
# Get the type of "Number" which is integer
type(Number)
int

1.2 Math Operations
# Define a variable named Ticket and initialize it with 5 indicating that we own 5 Ticket
# Let's assume that we bought an additional Ticket so now we have 6 Ticket instead of 5 
# We can increment Ticket by 1 stock as follows:
Ticket = 5
Ticket = Ticket + 1 
Ticket 
6
# Alternatively, we can increment the variable Ticket by 1 as follows:
Ticket = 5
Ticket += 1 
Ticket
6
# Let's assume that the price of Ticket price is 260 and we currently have 5 Ticket in our portfolio
# We can calculate the total value as follows:
Ticket_count = 5
Ticket_price = 260
Total_Value = Ticket_count * Ticket_price
Total_Value
1300

1.3 Order of Operations (Precedence)
# add 3 and 4 and then multiply the answer by 5
# Note that parentheses have the highest precedence
(3 + 4) * 5
35
# add 3 and 4 and then multiply the answer by 5
# Note that parentheses have the highest precedence
(3 + 4) * 5
7
# Division has higher precedence compared to addition
16 / 4 + 25 / 5
9.0

1.4 Print Operations
# Print function is used to print elements on the screen
# Define a string x 
# A string in Python is a sequence of characters
# String in python are surrounded by single or double quotation marks
x = "Python Tutorial"
# Obtain the data type for 'x'
type(x)
str
# Print x
print(x)
Python Tutorial
# Define a string and put 'Python' in it
print(x)
company_name = "Python."
# The format() method formats the specified value and insert it in the placeholder. 
# The placeholder is defined using curly braces: {}
print("Ticker name for {} is AAPL".format(company_name))
Ticker name for Python. is AAPL"
shares = 1
# You can place two or more placeholders with print function 
print("I own {} device of {}".format(shares, company_name))
I own 1 device of Python.

1.5 Get User Input
# input is a built-in function in python
# Get bank client name as an input and print it out on the screen 
name = input("Welcome to the bank, What's your name: ")
print('Hello', name)
Welcome to the bank, What's your name: Tushar
Hello Tushar
# Obtain bank client data such as name, age and assets and print them all out on the screen 
name = input("Welcome to the bank, please enter your name: ")
age = input("Which tutoiral is this: ")
print("My name is {}, This is {} tutorial ".format(name, age))
Welcome to the bank, please enter your name: Tushar
Which tutoiral is this: python
My name is Tushar, This is python tutorial
# Obtain user inputs, perform mathematical operation, and print the results 
# Note: Are you getting an error? Move to the next code cell  
x = input("Enter the price of AAPL stock today: ")
y = input("Enter the number of APPLE stocks that you want to buy: ")
print('The total funds required to buy {} shares of AAPL stock at {} dollars is {}'.format(y, x, x*y)) 






# Convert from string datatype to integer datatype prior to performing addition 
x = input("Enter the price of AAPL stock today: ")
x = int(x)
y = input("Enter the number of APPLE stocks that you want to buy: ")
y = int(y)
print('The total funds required to buy {} shares of AAPL stock at {} dollars is {}'.format(y, x, x*y))
Enter the price of AAPL stock today: 2
Enter the number of APPLE stocks that you want to buy: 3000
The total funds required to buy 3000 shares of AAPL stock at 2 dollars is 6000


2 Data Types

Data types are the classification or categorization of data items.

2.1 BOOLEANS
# Boolean values can be represented as one of two constant objects "False" and "True". 
# Booleans behave like integers 0 and 1.
True
True
a = 100
b = 200
print(a == b)
False
a = 100
b = 200
print(a > b)
False

2.2 List
# A list is a collection which is ordered and changeable. 
# List allows duplicate members.
my_list = [1, 2, 3]
my_list
[1, 2, 3]
# Obtain the datatype
type(my_list)
list
#  You can have a list inside another list (nested list)
my_list = ["GOOG", [3, 6, 7]]
my_list
['GOOG', [3, 6, 7]]
# Access specific elements in the list with Indexing
# Note that the first element in the list has an index of 0 (little confusing but you'll get used to it!)
my_list = [2, "GOOG", 5]
my_list[1]
'GOOG'
# indexing for nested lists
my_list = ["GOOG", [3, 6, 7], ["APPL", 5, 6]]
my_list[2][0]
'APPL'
print(my_list[1][1])
6
print(my_list[2])
['APPL', 5, 6]
# Negative Indexing
my_list = [2, "GOOG", 5]
print(my_list[-2])
GOOG
# List Slicing (getting more than one element from a list) 
my_list = [2, 5, 7, "GOOG", "SP500", "AMZN", 15]
      
      # obtain elements starting from index 3 up to and not including element with index 6  
print(my_list[3:6])
['GOOG', 'SP500', 'AMZN']
# Obtain elements starting from index 3 to end
print(my_list[3:])
['GOOG', 'SP500', 'AMZN', 15]
# All elements start to end
print(my_list[:])
[2, 5, 7, 'GOOG', 'SP500', 'AMZN', 15]
# Obtain the length of the list (how many elements in the list) 
len(my_list)
7

3 Python Operators

Operators in python are used to perform arithmetic & logical operations.

3.1 COMPARISON OPERATORS

# Comparison Operator output could be "True" or "False"
# Let's cover equal '==' comparison operator first
# It's simply a question: "Is price1 equals price2 or not?"
# "True" output means condition is satisfied 
# "False" output means Condition is not satisfied (condition is not true) 
price1 = 1000
price2 = 1000
price1 == price2
True
# Greater than or equal operator '>=' 
price1 = 5000
price2 = 1000
price1 >= price2
True
# Note that '==' is a comparison operator 
# Note that '=' is used for variable assignment (put 10 in price1) 
price1 = 10

# Both strings have to exactly match (lower case vs. upper case) 
'Finance' == 'finance'
False
# Not equal comparison operator '!=' 
price1 = 1000
price2 = 1000
price1 != price2
False

3.2 Logical Operators
# For "and" logical operators, both arguments must be "True" in order for the output to be "True" 
# (Note that this is the only case in which the 'and' logical operator generates a True output)
# 1 and 1 = 1 
True and True
True
# 0 and 1 = 0
False and True 
False
# 0 and 0 = 0
False and False 
False
# For "or" logical operators, if any one of the arguments is "True", the output will be "True" 
# 1 or 1 = 1
True or True  
True
# 1 or 0 = 1
True or False  
True
# 0 or 1 = 1
False or True
True
# 0 or 0 = 0 (Note that this is the only case in which the 'or' logical operator generates a False output)
False or False 
False
#True and False = False 
(10>3) and (9==3)
False
#True or False = True 
(9>3) or (12==10)
True

3.3 Conditional Statements A simple if-else statement is written in Python as follows:
if condition:
  statement #1
else:
  statement #2
  • If the condition is true, execute the first indented statement
  • if the condition is not true, then execute the else indented statements.
  • Note that Python uses indentation (whitespace) to indicate code sections and scope.

if 10 > 9:
    print('If condition is True')
else:
    print('If condition is False')
If condition is True
if 5 == 10:
    print('If condition is True')
else:
    print('If condition is False')
If condition is False
if 5 == 7:
    print('First statement is true')
elif 2 == 4:
    print('Second statement is true')
else:
    print('Last statement is true')
Last statement is true
if 5 = 7:
    print('First statement is true')
elif 2 > 4:
    print('Second statement is true')
else:
    print('Last statement is true')
Last statement is true
# Let's take an input from the user and grant or deny access accordingly
name = input('Enter your username:')

if name == 'Tushar':
    print("Access granted")
else:
    print("Access Denied")
Enter your username:Tushar
Access granted