Pytho
n
• functional and structured programming methods as well
as OOP
Hello World
# Python code to print "Hello, World!"
print("Hello, World!")
# \n Not required for new line
print("Hello", end=" ")
print("World")
Comment
• #This is single line comment.
• '''
This is
Multi line comment.
'''
Variables
• a = str(123)
• b = int(123)
• c = float(123)
• name, age, city = "Kelly Hu", 27, "Brentwood"
String
• test = "Hello, World!“
print(test[2:5])
print(test[0:])
print(test[:-1])
print(test[:5])
print(test[:])
• name = "Kelly Hu“
age = 27
person = f"My name is {name}, I am {age} years old"
String Concatenation
a = "My Name"
print(a + "is Shrey")
Sequence Types
• List
a = [1, 2, 3, 4, 5]
• Tuple
a = (1, 2, 3, 4, 5)
• Dictionary
a = {"Name" : "John", "Age" : 32}
User Input
• name = input("Enter your name: ")
print("Hello", name)
Condition
a = 10
b = 20
if a > b:
print("a is greater than b")
elif b > a:
print("b is greater than a")
else:
print("a is greater than b")
Loops
students = ["Kelly Hu", "Akanksha", "Peter"]
for s in students:
print(s)
i = 1
while i <= 10:
print(i) i += 1