#----------------------------------------------------------
# hello world program
# by: John Doe
# Jan 1, 2017
# Copyright: Anyone may freely copy or modify this program
#----------------------------------------------------------
print('hello world!') # say hello to the beautiful world!
# In python 2: print is a statement not a function
# print 'Hello World!'
hello world!
type(100)
int
type(-9)
int
type(1000.99345435)
float
type(-2.345)
float
type('Hello World!')
str
type('A')
str
print("hello")
hello
type("17")
str
type("""Triple double quoted data""")
str
type('''Type of Triple single quote data is''')
str
a = "hello"
a
'hello'
data = 'hello' # can't conver it to int or float types
type(data)
str
data = '100'
type(data)
str
data
'100'
num = int(data)
type(num)
int
num
100
price = float('500.99')
type(price)
float
float('hi')
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-21-4db454aaf92e> in <module> ----> 1 float('hi') ValueError: could not convert string to float: 'hi'
num = 99.99
strNum = str(num)
type(strNum)
str
type(True)
bool
type(False)
bool
1+2
3
len('hello')
5
print(2+3*4)
14
print('''"Oh no", she exclaimed, "Ben's bike is broken!"''')
"Oh no", she exclaimed, "Ben's bike is broken!"
print(34.55)
34.55
print(2+2)
4
print('What\'s up\n Shaq O\'Neal?')
What's up Shaq O'Neal?
print('hello \there...\n how are you?')
hello here... how are you?
print('how many back slahses will be printeted? \\\\')
how many back slahses will be printeted? \\
print(r'how many back slahses will be printeted? \\\\')
how many back slahses will be printeted? \\\\
help('keywords')
Here is a list of the Python keywords. Enter any keyword to get more help. False class from or None continue global pass True def if raise and del import return as elif in try assert else is while async except lambda with await finally nonlocal yield break for not
# variable must be defined/declared before you can use
print(x)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-36-293ab258265c> in <module> 1 # variable must be defined/declared before you can use ----> 2 print(x) NameError: name 'x' is not defined
x = 'some value'
print(x)
some value
# Exercise: Define a bunch of variables to store some values of different types
var1 = -100
num = -99.99
name = 'John'
lName = 'Smith'
MI = 'A'
grade = 10.5
Name = 'Jake'
grade = 19.9
var = 100
var = 'hello'
var = 99.89
var
99.89
# Exercise: play with some examples of various operators supported by Python
uses PEMDAS rule from high to low order
# some examples
print(2 * 3 ** 2)
18
x = 1
y = 2
z = 3
ans = x+y-z*y**y
print(ans)
-9
# some examples
fname = "John"
lname = "Smith"
fullName = fname + lname
print(fullName)
JohnSmith
gene = "AGT"*10
print(gene)
AGTAGTAGTAGTAGTAGTAGTAGTAGTAGT
name = input('What is your name? ')
What is your name?
print('hello,', name)
hello,
num = input('Enter a number =>')
print('You entered: ', num)
print('type of', num, '=', type(num))
Enter a number => You entered: type of = <class 'str'>
num
''
# str must be casted into int or float depending on the value required
num = int(num)
print('type of', num, '=', type(num))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-54-1935f54633b6> in <module> 1 # str must be casted into int or float depending on the value required ----> 2 num = int(num) 3 print('type of', num, '=', type(num)) ValueError: invalid literal for int() with base 10: ''
# Demonstrate composition step by step
# Algorithm steps
# 1. Get length and width of a rectangle
# a. hard coded values OR
# b. prompt user to enter length and width values
# i. convert length and width into right data types
# 2. Find area = length x width
# 3. Find perimeter = 2(length + width)
# 4. Display calculated results
# Demonstrate composition step by step
# Algorithm steps