0% found this document useful (0 votes)
2 views

Python

The document provides a comprehensive overview of basic programming concepts, including printing, variables, data types, strings, numbers, user input, lists, tuples, functions, conditionals, loops, dictionaries, and file handling. It includes code snippets demonstrating each concept, such as a basic calculator, a guessing game, and a translator function. Additionally, it covers error handling and file reading/writing operations.

Uploaded by

m.mahavarsha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python

The document provides a comprehensive overview of basic programming concepts, including printing, variables, data types, strings, numbers, user input, lists, tuples, functions, conditionals, loops, dictionaries, and file handling. It includes code snippets demonstrating each concept, such as a basic calculator, a guessing game, and a translator function. Additionally, it covers error handling and file reading/writing operations.

Uploaded by

m.mahavarsha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

print("hello world")

drawing a shape
print(" /|")
print(" / |")
print(" / |")
print("/___|")

variables and data types


character_name = "Tom"
character_age = "50"
print("There once was a man named " + character_name + ", ")
print("he was " + characte_age + " years old. ")
print("he really liked the name " + character_name + ", ")
print("but didn't like being " + character_age + ".")

working with strings


phrase = "Giraffe Academy"
print(phrase)
print(phrase.lower())
print(phrase.upper())
print(phrase[0])
print(phrase.replace"giraffe", "Elephant")

working with numbers


my_num = 5
print(str(my_num) + " my favorite number")
my_num = -5
print(pow(4, 6))

print(max(4, 6))

print(min(4, 6))

print(round(3.2))

print(ceil(3.7))

print(sqrt(36))

getting input from users


name = input("Enter your name: ")
age = input("Enter your age: ")
print("Hello " + name + "! you are " + age)

building a basic calculator


num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
result = num1 + num2
print(result)

result = int(num1) + int(num2)


print(result)

result = float(num1) + float(num2)


print(result)

mad libs game


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

print("roses are " + colour)


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

lists
friends = ["kevin", "karen", "jim"]

print(friends[0])

list functions
lucky_numbers = [4, 8, 15, 16, 23, 42]
friends = ["kevin", "karen", "jim", "oscar", "tom"]
friends.extend(lucky numbers)
print(friends)

lucky_numbers = [4, 8, 15, 16, 23, 42]


friends = ["kevin", "karen", "jim", "oscar", "tom"]
friends.append("creed")
print(friends)

lucky_numbers = [4, 8, 15, 16, 23, 42]


friends = ["kevin", "karen", "jim", "oscar", "tom"]
friends.insert(1, "kelly")
print(friends)

lucky_numbers = [4, 8, 15, 16, 23, 42]


friends = ["kevin", "karen", "jim", "oscar", "tom"]
friends.remove("jim")
print(friends)

lucky_numbers = [4, 8, 15, 16, 23, 42]


friends = ["kevin", "karen", "jim", "oscar", "tom"]
friends.clear()
print(friends)

lucky_numbers = [4, 8, 15, 16, 23, 42]


friends = ["kevin", "karen", "jim", "oscar", "tom"]
friends.pop()
print(friends)

lucky_numbers = [4, 8, 15, 16, 23, 42]


friends = ["kevin", "karen", "jim", "oscar", "tom"]
print(friends,index("kevin"))

lucky_numbers = [4, 8, 15, 16, 23, 42]


friends = ["kevin", "karen", "jim", "jim", "oscar", "tom"]
print(friends.count("jim"))

lucky_numbers = [42, 8, 15, 16, 23]


friends = ["kevin", "karen", "jim", "oscar", "tom"]
luck_numbers.sort()
print(lucky_numbers)

lucky_numbers = [42, 8, 15, 16, 23]


friends = ["kevin", "karen", "jim", "oscar", "tom"]
luck_numbers.reverse()
print(lucky_numbers)
lucky_numbers = [42, 8, 15, 16, 23]
friends = ["kevin", "karen", "jim", "oscar", "tom"]
friends2 = friends.copy()
print(friends)

tuples
coordinates = [(4, 5), (6, 7), (80, 34)]
coordinates[1] = 10
print(coordinates[1])

functions
def say_hi(name, age):
print("hello " + name + ", you are " + str(age))

say_hi("mike", 35)
say_hi("steve", 70)

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

result = cube(4)
print(result)

if statements

is_male = true
is_tall = ture

if is_male or is_tall:
print("you are a male or tall or tall or both")
else:
print("you neither male nor tall")

is_male = false
is_tall = false

if is_male or is_tall:
print("you are a male or tall or tall or both")
else:
print("you neither male nor tall")

is_male = true
is_tall = ture

if is_male and is_tall:


print("you are a tall male")
else:
print("you either not male or not tall or both")

is_male = true
is_tall = ture

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 either not male or not tall or both")

is_male = true
is_tall = ture

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 not a male not tall")

if statements and comparison

def max_num(num1, num2, num3):


if num1 <= num2 and num1 >= num3:
return num1
elif num2 >= num1 and num2 >= num3:
return num2
else:
return num3

print(max_num(300, 40, 5))

bulding a better calculator

num1 = float(input("Enter first number: "))


op = input("Enter operator: ")
num2 = float(input("Enter second number: "))

if op == "+":
print(num1 + num2)
elif op == "-":
print(num1 - num2)
elif op == "/":
print(num1 / num2)
elif op == "*":
print(num1 * num2)
else:
print("invalid operator")

dictionaries

monthconversions = {
"Jan": "January",
"Feb": "Febrauary",
"Mar": "March",
"Apr": "April",
"May": "May",
"Jun": "June",
"Jul": "July",
"Aug": "August",
"Sep": "September",
"Oct": "October",
"Nov": "November",
"Dec": "December",
}

print(monthcoversions.get("Luv", "Not a valid key"))

while loop

i = 1
while i <= 10:
print("Done with loop")

building a guessing game

secret_word = "giraffe"
guess = ""
guess_count = 0
guess_limit = 3
out_of guesses = False

while guess != secret_word and not(out of guesses)


if guess_count < guess_limit:
guess = inuput("Enter guess: ")
guess_count += 1
else:
out_of_guesses = true

if out_of_guesses:
print("Out of Guesses, YOU LOSE!")
else:
print("You Win!")

For loop
friends = ["jim", "karen", "kevin"]

for index in range(5):


if index == 0:
print("first interation")
else:
print("not first")

exponent function

def raise_to_power(base_num, Pow_num):


result = 1
for index in range(pow_num):
result = result * base_num
return result

print(raise_to_power(2,3))

2d lists and nested loops

number_grid = [
[1,2,3,],
[4,5,6,],
[7,8,9,],
[0]
]
for row in number_grid:
for col in row:
print(col)

buildinga translator

def translate(phrase):
translation = ""
for letter in phrase:
if letter.lower() in "aeiou":
if letter.isupper():
translation = translation + "G"
else:
translation = translation + "g"

else:
translation = translation + letter
return translation

print(translate(input("Enter a phrase: ")))

comments
# print("Comments are fun")

try exept

try:
value = 10/ 0
number = int(input("Enter a number: "))
print(number)
except ZeroDivisionError:
print("Divided by zero")
except valueError:
print("invalid input")

try:
answer = 10/ 0
number = int(input("Enter a number: "))
print(number)
except ZeroDivisionError as err:
print("err")
except valueError:
print("invalid input")

reading files

employee_file = open("employees.txt", "r")


for employee in employee_files.readlines():
print(employee)
employee_file.close()

writing to files
employee_file = open("employees.txt", "r")
print(employee_file.read())
employee_file.close()

You might also like