print("Hello world")
Hello world
Define variables in python
# define a variable to store number
number = 10
print(number)
10
# define a variable to store string value
name = "John Smith"
print(name)
John Smith
# concatenate variables and display values
print("Hello " + name + ", the number is " + str(number))
Hello John Smith, the number is 10
print("Hello")
print("World")
Hello World
tv = 15
mobile = 20
tablet = 30
# use \ to indicate codes continue in the next line
total = tv + \
mobile + \
tablet
print(total)
65
# indentation
n = 9
r = 1
# while loop
while n > 0 :
r = r * n
n = n - 1
# print values after loop completed
print(r)
362880
str1 = 'hello'
str2 = "hello"
str3 = """hello
world
"""
str4 = '''hello
world
'''
print(str1)
print(str2)
print(str3)
print(str4)
hello
hello
hello
world
hello
world
age = 20; print(age); print("Age : " + str(age))
20 Age : 20
print("Hello \nWorld")
Hello World
print("Hello\tWorld")
Hello World
print('Hello\tWorld')
Hello World
name = input("Enter name")
print(name)
# get a number and convert to integer
number = int(input("Enter a new number"))
print(number)
total = number * 2
print(total)
# repeat the string value
print(name * 3)
print('-' * 20)
34 55 110 343434 --------------------
print(name + str(number))
3455
# define 3 variables at the same and all have same value
a = b = c = 99
print(a)
print(b)
print(c)
a = 88
print(a)
print(b)
print(c)
99 99 99 88 99 99
# define 3 variables at the same and all have different values
name, email, age = "John", "john@example.com", 30
print(name + " " + email + " " + str(age))
John john@example.com 30
print("pi = %s" % "3.14159")
pi = 3.14159
print("pi = %.5f" % 3.14159)
pi = 3.14159
print("pi is <%-6.2f>" % 3.14159)
print("pi is <%6.2f>" % 3.14159)
pi is <3.14 > pi is < 3.14>
# using the % to do formatting sequences
(name, email, age) = ("John", "john@example.com", 30)
print("Name : %s, Email : %s, Age : %d" % (name, email, age))
print("%s\t%s\t%6.3f" % (name, email, age))
# assign new values to name, email and age
(name, email, age) = ("Bob", "bob@example.com", 35)
print("%s\t%s\t%6.3f" % (name, email, age))
Name : John, Email : john@example.com, Age : 30 John john@example.com 30.000 Bob bob@example.com 35.000
# use the format() with numerical index
print("Name : {0}, Email : {1}, Age: {2}".format(name, email, age))
print("Age: {0}, Name : {0}, Email : {1}".format(name, email, age))
Name : Bob, Email : bob@example.com, Age: 35 Age: Bob, Name : Bob, Email : bob@example.com
# use name instead
print("Name : {n}, Email : {e}, Age : {a}".format(n=name, e=email, a=age))
print("Age : {a}, Name : {n}, Email : {e}".format(n=name, e=email, a=age))
Name : Bob, Email : bob@example.com, Age : 35 Age : 35, Name : Bob, Email : bob@example.com
print("{0:10} is the food of gods".format("Ambrossia"))
print("{0:10} is the food of gods".format("Ambrossiaccc"))
print("{0:{1}} is the food of gods".format("Ambrossia", 10))
print("{food:{width}} is the food of gods".format(food="Ambrossia", width=10))
print("{0:>10} is the food of gods".format("Ambrossia")) # right-align
print("{0:&>10} is the food of gods".format("Ambross")) # right-align, replace whitespace with &
Ambrossia is the food of gods Ambrossiaccc is the food of gods Ambrossia is the food of gods Ambrossia is the food of gods Ambrossia is the food of gods &&&Ambross is the food of gods
# named parameters and formatting sequences
num_dict = {'e' : 2.718, 'pi' : 3.14159}
print("%(pi).2f - %(pi).4f - %(e).2f" % num_dict)
3.14 - 3.1416 - 2.72
import time
localtime = time.asctime(time.localtime(time.time()))
print("Formatted time : ", localtime)
Formatted time : Fri Feb 26 12:03:12 2021
import calendar
calendar.prcal(2021)
2021
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 7
4 5 6 7 8 9 10 8 9 10 11 12 13 14 8 9 10 11 12 13 14
11 12 13 14 15 16 17 15 16 17 18 19 20 21 15 16 17 18 19 20 21
18 19 20 21 22 23 24 22 23 24 25 26 27 28 22 23 24 25 26 27 28
25 26 27 28 29 30 31 29 30 31
April May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 1 2 1 2 3 4 5 6
5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13
12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20
19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27
26 27 28 29 30 24 25 26 27 28 29 30 28 29 30
31
July August September
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 1 1 2 3 4 5
5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12
12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19
19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26
26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30
30 31
October November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5
4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12
11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19
18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26
25 26 27 28 29 30 31 29 30 27 28 29 30 31
# conditional statements
x = 5
if x == 5 :
print("equal 5")
elif x > 5 :
print("greater than 5")
else :
print("less than 5")
equal 5
# iterate 1 to 5 and display value
for v in range(1, 6) :
print(v)
1 2 3 4 5
for v in range(5) :
print(v)
0 1 2 3 4
for v in range(2, 30, 2) :
print(v)
2 4 6 8 10 12 14 16 18 20 22 24 26 28
# list of fruit names
fruits = ["apple", "banana", "cherry"]
for f in fruits :
print(f)
apple banana cherry
name = "John Smith"
for s in name :
print(s)
J o h n S m i t h
# stop looping when f is banana - break : exit from the current block
for f in fruits :
print(f)
if f == "banana" :
break
apple banana
# continue : go to next iteration/item
for f in fruits :
if f == "banana" :
continue
print(f)
apple cherry
# while-loop 1 to 5
i = 1
while i <= 5 :
print(i)
i += 1
1 2 3 4 5
i = 1
while i <= 5 :
if i == 3 :
break
print(i)
i += 1
1 2
i = 1
while i <= 5 :
i += 1
if i == 3 :
continue
print(i)
2 4 5 6
# use while loop to iterate fruits
i = 0
while i < len(fruits) :
print(fruits[i])
i += 1
apple banana cherry
number = "abc" # user input - string
try :
total = int(number) + 2
except :
print("cannot convert to int")
cannot convert to int
# example of string slicing
title = "Python Data Science"
print(title)
subtitle = title[0:5]
print(subtitle)
subtitle = title[5:]
print(subtitle)
subtitle = title[:5]
print(subtitle)
subtitle = title[5:10]
print(subtitle)
subtitle = title[-5:]
print(subtitle)
subtitle = title[:-5]
print(subtitle)
Python Data Science Pytho n Data Science Pytho n Dat ience Python Data Sc
subtext = "Python"
print(subtext in title)
subtext = "python"
print(subtext in title)
subtext = "python"
print(subtext not in title)
True False True
# use lambda to define anonymous function
sum = lambda val1, val2 : val1 + val2
# call sum function and display result
print("Total: ", sum(1, 2))
Total: 3
# use python syntax to define function
def add(val1, val2) :
return val1 + val2
print("Total: ", add(1, 2))
Total: 3