Python – Chapter 1

 

1

Hello World

print("Hello World!")

“” – quotation mark

( ) – parentheses

! – Exclamation mark

 

2

Variables and Data types

– Variables can be used for storing Data (strings, numbers or True/ False)

print("My name is Sang")

print("I'm 30 years old")

– Add variables

character_name = "Sang"

character_age = "30"

print("My name is " + character_name)

print("I'm " + character_age + " years old")

 

3

Working with strings

1 – \n = enter

\ – backslash

print("IELTS\n Vuive")

2- functions

phrase = "IELTS Vuive "
print(phrase + "is fun")
phrase = "IELTS Vuive "
print(phrase.lower())
phrase = "IELTS Vuive "
print(phrase.upper())
phrase = "IELTS Vuive "
print(phrase.islower())
phrase = "IELTS Vuive "
print(phrase.isupper())
phrase = "IELTS Vuive "
print(phrase[6])
phrase = "IELTS Vuive "
print(phrase.index("V"))

 

4

Working with numbers

my_num = 777222169
print("0" + str(my_num) + " is my phone number")
print(pow(3,2)) 

= 9
print(max(4,6))

= 6

print(min(4,6))

= 4
print(round(6.5))

= 6

from math import *

print(floor(3.7)) = 3

print(ceil(3.7)) = 4

print(sqrt(9)) = 3.0

 

5

Getting input from users

name = input("Enter your name: ")
email = input("Enter your email: ")
print("Hello " +name + "!")
print("Your email is " +email)

 

6

Building a basic calculator

numb1 = input("Enter a number: ")
numb2 = input("Enter another number: ")
result = float(numb1) + int(numb2)

print(result)

 

7

Mad libs games

color = input("Enter a color: ")
plural_noun = input("Enter a plural noun: ")
celebrity = input("Enter a celebrity: ")

print("Roses are " + color)
print(plural_noun + " are blue")
print("I love " + celebrity)

 

8

Lists

friends = ["Kevin", "Karen", "Jim"]
friends[1] = "Mike"
print(friends[0:3])

 

9

List functions

lucky_numbers = [4, 8, 15, 16, 23, 42]
friends = ["Kevin", "Karen", "Jim", "Oscar", "Tony"]
friends.sort()
lucky_numbers.reverse()
friends.extend(lucky_numbers)
friends.append("Creed")
friends.insert(1, "Sang")
friends.remove("Jim")
friends.pop()
friends = friends.copy()
print(friends)
print(friends.index("Sang"))
print(friends.count("Tony"))

 

10

Tuples – cannot be modified

coordinates = (4, 5)

 

11

Functions

def say_hi(name, age):
    print("Hello " + name + ", you are " + age)

say_hi("Mike", "35")
say_hi("Steve", "70")

 

12

Return statements

def cube(num):
    return num*num*num

result = cube(4)
print(result)

 

13

If statements

is_male = True
is_tall = False
if is_male and is_tall:
    print("You are a tall male")
elif is_male and not(is_tall):
    print("You are a short male")
elif not(is_male) and is_tall:
    print("You are not a male but are tall")
else:
    print("You are neither male nor tall")

 

14

If statements and Comparisons