# location = {0:"You are Sitting in front of comp",
# 1:"You are in road",
# 2:"At the top of the hill",
# 3:"Building",
# 4:"You are across Valley",
# 5:"Roaming at forest"}
# exits = [{"Q":0},{"N":5,"S":4,"E":3,"Q":0},{"N":5,"Q":0},{"W":1,"Q":0},
{"W":2,"N":1,"Q":0},{"S":1,"W":2,"Q":0}]
# loc=1
# while True:
# availablexits = ""
# #we can use join to do the fllowing
# # for direction in exits[loc].keys():
# # availablexits +=direction+ ","
# availablexits = ','.join(exits[loc].keys())
#
# print(location[loc])
#
# if loc == 0:
# break
#
# direction = input("Available Exits are "+ availablexits).upper()
# print()
# if direction in exits[loc]:
# loc = exits[loc][direction]
# else:
# print("You cannot go in that direction")
#the 1st challenge here is to convert the exit list to dictionary,
#list index can be converted to dictionary ,by making the list index no
# to corresponding dictionary key in this case
#
#
# location = {0:"You are Sitting in front of comp",
# 1:"You are in road",
# 2:"At the top of the hill",
# 3:"Building",
# 4:"You are across Valley",
# 5:"Roaming at forest"}
# exits = {0:{"Q":0},
# 1:{"N":5,"S":4,"E":3,"Q":0},
# 2:{"N":5,"Q":0},
# 3:{"W":1,"Q":0},
# 4:{"W":2,"N":1,"Q":0},
# 5:{"S":1,"W":2,"Q":0}}
# loc=1
# while True:
# availablexits = ""
# #we can use join to do the fllowing
# # for direction in exits[loc].keys():
# # availablexits +=direction+ ","
# availablexits = ','.join(exits[loc].keys())
#
# print(location[loc])
#
# if loc == 0:
# break
#
# direction = input("Available Exits are "+ availablexits).upper()
# print()
# if direction in exits[loc]:
# loc = exits[loc][direction]
# else:
# print("You cannot go in that direction")
#the 2nd challenge is to make use of the vocabulary,
#ie user may not type in the exact letters like N,S,W,E,
#user can type north,south as well
#
# location = {0:"You are Sitting in front of comp",
# 1:"You are in road",
# 2:"At the top of the hill",
# 3:"Building",
# 4:"You are across Valley",
# 5:"Roaming at forest"}
# exits = {0:{"Q":0},
# 1:{"N":5,"S":4,"E":3,"Q":0},
# 2:{"N":5,"Q":0},
# 3:{"W":1,"Q":0},
# 4:{"W":2,"N":1,"Q":0},
# 5:{"S":1,"W":2,"Q":0}}
#
# vocabularyvar = {"North":"N","South":"S","East":"E","WEST":"W","QUIT":"Q"}
#
# loc=1
# while True:
# availablexits = ""
# #we can use join to do the fllowing
# # for direction in exits[loc].keys():
# # availablexits +=direction+ ","
# availablexits = ','.join(exits[loc].keys())
#
# print(location[loc])
#
# if loc == 0:
# break
#
# direction = input("Available Exits are "+ availablexits)
# if len(direction)>0:
# if direction in vocabularyvar:
# direction = vocabularyvar[direction].upper()
# print(direction)
# print()
# if direction in exits[loc]:
# loc = exits[loc][direction]
# else:
# print("You cannot go in that direction")
#
# split command,will split the string by default by space,the result will be of list type
# location = {0:"You are Sitting in front of comp",
# 1:"You are in road",
# 2:"At the top of the hill",
# 3:"Building",
# 4:"You are across Valley",
# 5:"Roaming at forest,which is wild"}
#
# print(location[0])
# print(location[0].split())
# print(location[5].split(","))
# print(" ".join(location[0].split()))
#using the split command in the above example and checking whether one of the user entered words,
#matches with the exits,like if the user enters "I Prefer South"/"I want to go north"
location = {0:"You are Sitting in front of comp",
1:"You are in road",
2:"At the top of the hill",
3:"Building",
4:"You are across Valley",
5:"Roaming at forest"}
exits = {0:{"Q":0},
1:{"N":5,"S":4,"E":3,"Q":0},
2:{"N":5,"Q":0},
3:{"W":1,"Q":0},
4:{"W":2,"N":1,"Q":0},
5:{"S":1,"W":2,"Q":0}}
vocabularyvar = {"NORTH":"N","SOUTH":"S","EAST":"E","WEST":"W","QUIT":"Q"}
loc=1
while True:
availablexits = ""
#we can use join to do the fllowing
# for direction in exits[loc].keys():
# availablexits +=direction+ ","
availablexits = ','.join(exits[loc].keys())
print(location[loc])
if loc == 0:
break
direction = input("Available Exits are "+ availablexits).upper()
if len(direction)>0:
words = direction.split()
for word in words:
if word in vocabularyvar:
direction = vocabularyvar[word]
print(direction)
print()
if direction in exits[loc]:
loc = exits[loc][direction]
else:
print("You cannot go in that direction")
Saturday, 17 February 2018
Python Dictionaries Challenge - Day 8(After 1 Week)
Saturday, 10 February 2018
Python Dictionaries part1 - Day 7
#dictionary can not be accessed vy index,but through key value
films = {"muthu":"one of the movie in which rajini comes in double role",
"basha":"Don of a don movie,a cult hit",
"enthiran":"rajini's scifi movie",
"murattu kalai":"movie taken in paganeri"}
print(films)
print(films["murattu kalai"])
#to add a new value to dictionay we dont have a method or function,but we can assign like the below one
films["kaala"]="more like a basha 2,april 27th - 2018 release"
print(films)
#if we use the same key and assign it will update instead of creating new element
films["kaala"]="probably thalaivars gonna be biggest hit"
print(films)
#similarly it will take the last set of key,value combination if there is any duplicate
films = {"muthu":"one of the movie in which rajini comes in double role",
"basha":"Don of a don movie,a cult hit",
"enthiran":"rajini's scifi movie",
"murattu kalai":"movie taken in paganeri",
"muthu":"one of the thalaivar & sarath babu combination movie"}
print (films["muthu"])
bike = {"make":"royal enfiled","model":"himalayan","cc":400,"review":"mostly avg or bad"}
print (bike["model"])
print (bike["cc"])
#delete a element or delete a entire dictonary or cleare the elements in the dictionary
del(bike["review"])
# del(bike)
# bike.clear()
print (bike)
#getting the unpresent key value will result in error,in that case we can use get function
# print (bike["color"])
print(bike.get("color"))
#while True is used to make the user keep qasking the questions,unless he types quit
# while True:
# x = input("enter any thalaivar's film name: ")
# if x == "quit":
# break
# description = films.get(x)
# print(description)
# #the below piece of code will return error if we enter any unknown key value
# print(films[x])
#same code is rewritten to print the custom message if the value doesnot exists in dictory
while True:
x = input("enter any thalaivar's film name: ")
if x == "quit":
break
if x in films:
description = films.get(x)
print(description)
print(films[x])
else:
print ("{} - doesnot exists in dictionaryy".format(x))
Python Handling Binary ,Hex and octal Numbers - Day 6(posted next day)
# #decimal numbers in binary
# for i in range(10):
# print("{0:>2} in binary is {0:>8b}".format(i))
#
# print(0b1011)
# #hex decimal
# for i in range(257):
# print("{0:>2} in hex is {0:>02x}".format(i))
#
# #hex multipliation
# x = 0x20
# y = 0x0a
# print(x*y)
# #operations like or,and,xor,add,subtract refreshed
#converting decimal to binary through program
# print(10//2)
# print(10%3)
# powers = []
# for power in range(15,-1,-1):
# powers.append(2**power)
# # print(powers)
# print(powers)
# x = int(input("enter any number less than 65535 to convert to binary \n"))
# for i in powers:
# # print(i)
# print(x//i,end ='')
# x%=i
#the above program will work ,but to avoid the trialing 0's can use the below code
powers = []
for power in range(15,-1,-1):
powers.append(2**power)
# print(powers)
print(powers)
printing = False
x = int(input("enter any number less than 65535 to convert to binary \n"))
for i in powers:
# print(i)
bit = x//i
if bit!=0 or i ==1:
printing =True
if printing==True:
print(bit,end ='')
x%=i
Wednesday, 7 February 2018
Python Tuples - Day 5(posted next day)
# tuplevar1 = ("a","b","c")
# tuplevar2 = "a","b","c"
# print(tuplevar1)
# print(tuplevar2)
# print(("a","b","c"))
# print("a","b","c")
#
# #tuples are immutable objects,meaning they cannot be changed/altered ,
#they can only be assigned
# welcome = "hi","hello",2018
# print(welcome)
# print(welcome[0])
# #the below line of code will give an error ,since we are trying to alter the tuple
# # welcome[0]="hii"
# print(welcome)
#
# tupvar1 = "hi","ji"
# print(tupvar1)
# tupvar2 = welcome[1],tupvar1[0],2019
# print(tupvar2)
#
# #where as the list object can be altered like below
# listvar1 = ["hi","hello",2020]
# print(listvar1)
# listvar1[0]="Hii"
# print(listvar1)
#right side expression is evaluated first
# a , b = 1,2
# print(a,b)
# c=d=e=f=2
# print(c,d,e)
# a,b =b,a
# print(a,b)
#
# albumtuplevar1 = "muthu","rahman",2000
# print(albumtuplevar1)
# title,composer,year =albumtuplevar1
# print(title)
# print(composer)
# print(year)
#the below piece of code will throw an error like ValueError: not enough values
#to unpack (expected 4, got 3)
# var1,var2,var3,var4=albumtuplevar1
# print(var1)
# print(var2)
# print(var3)
# print(var4)
#the below piece of code will throw an error likeValueError:
#too many values to unpack (expected 2
# var1,var2=albumtuplevar1
# print(var1)
# print(var2)
#append work in tuple
# albumtuplevar1.append("Action")
#tuple inside tuple
# albumtuplevar1 = "muthu","rahman",2000,((1,"oruvan oruvan"),(2,"kuluvall"),(3,"vidukathaya"))
# print(albumtuplevar1)
# title,composer,year,tracks =albumtuplevar1
# print(title)
# print(composer)
# print(year)
# print(tracks)
# albumtuplevar2 = "muthu","rahman",2000,(1,"oruvan oruvan"),(2,"kuluvall"),(3,"vidukathaya")
# print(albumtuplevar2)
# title,composer,year,track1,track2,track3 =albumtuplevar2
# print(title)
# print(composer)
# print(year)
# print(track1)
# print(track2)
# print(track3)
#
# #printing the tracks w/o knowing the count
# title,composer,year,tracks =albumtuplevar1
# print(title)
# print(composer)
# print(year)
# for song in tracks:
# no,songtitle = song
# print("songno-{},songtitle-{}".format(no,songtitle))
# print(song)
#mutable object inside a tuple can be altered,like the below example the list
#inside the tuple can be changed
albumtuplevar3 = "muthu","rahman",2000,[(1,"oruvan oruvan"),(2,"kuluvall"),(3,"vidukathaya")]
print(albumtuplevar3)
print(albumtuplevar3[3])
albumtuplevar3[3].append((4,"thillana thillana"))
print(albumtuplevar3)
for song in albumtuplevar3[3]:
no,songtitle = song
print("songno-{},songtitle-{}".format(no,songtitle))
title,composer,year,tracks =albumtuplevar3
tracks.append((5,"kokku seva kokku"))
print(albumtuplevar3)
for song in tracks:
no,songtitle = song
print("songno-{},songtitle-{}".format(no,songtitle))
Python List part2 and range part1 - Day3(posted next day)
#list inside a list
# menu = [];
# menu.append(["egg","milk","spam"])
# menu.append(["egg","milk","spam","bacon"])
# menu.append(["egg","milk"])
#
# for menuilist in menu:
# if "spam" not in menuilist:
# print(menuilist)
# for menuitem in menuilist:
# print(menuitem)
#few examples of iterables are String,List
#iterator,for loop already handles this function automatically
stringvar = "12345asd"
my_iterator = iter(stringvar)
print(my_iterator)
print(next(my_iterator))
print(next(my_iterator))
print(next(my_iterator))
print(next(my_iterator))
print(next(my_iterator))
print(next(my_iterator))
print(next(my_iterator))
print(next(my_iterator))
#will print an error if it exceeds the last iterable
# print(next(my_iterator))
daysinweekvar = ["Sun","Mon","Tue","Wed","Thurs","Fri","Sat",]
# for i in daysinweekvar:
# print(i)
print(len(daysinweekvar))
daysinweekitervar = iter(daysinweekvar)
for i in range(0,len(daysinweekvar)):
print(next(daysinweekitervar))
print (range(0,100))
print (range(100))
print (list(range(0,100)))
print (list(range(100)))
print (list(range(0,100,2)))
oddvar1 = range(1,100,2)
oddlistvar1 = list(range(1,100,2))
print(oddvar1)
print(oddlistvar1)
print(oddvar1.index(9))
print(oddlistvar1.index(9))
print(oddvar1[2])
print(oddlistvar1[2])
sevens = range(7,10000,7)
var2 = int(input("enter any no less than 10k"))
for var3 in sevens:
if var2 == var3:
print("{} is divisble by 7".format(var2))
Monday, 5 February 2018
Python List part1 - Day2
# ipaddress = input("enter a ip address \n")
# print (ipaddress.count("."))
#
# parrot_list = ["no more","a stiff"]
# print (parrot_list)
# parrot_list.append("green")
# print (parrot_list)
#
# for var in parrot_list:
# print (var)
#
# evenno = [2,4,4,6,8]
# odd = [1,3,5,7]
#
# print (evenno + odd)
# print (sorted(evenno + odd))
# numbers = evenno + odd
# #if u try the below code it wont return the sorted values,
# #because it will update the list and returns none as result
# #you can use sorted function if it has to be sorted at the time,but not
# #the original list itself
# # print (numbers.sort())
# numbers.sort()
# print (numbers)
# unsortednos = evenno + odd
# sortednos = (sorted(evenno + odd))
#
# #comparison wont be equal even if we have the same list items but in different order
# if sortednos == unsortednos:
# print ("equal")
# else:
# print ("not equal")
#
#
# if sortednos == sorted(unsortednos):
# print ("equal")
# else:
# print ("not equal")
# list_1= []
# list_2 = list()
# print ("list1 : {}".format(list_1))
# print ("list2 : {}".format(list_2))
#
# if list_1 == list_2:
# print ("equal")
# else:
# print ("not equal")
#
# print (list("welcome to the world of lists"))
#list is a constructor
# evenno = [2,4,6]
# anotherevenno = evenno
# print(anotherevenno is evenno)
# anotherevenno.sort(reverse=True)
# #below will print the same ,although we changed the different list,because
# #they are same
# print(evenno)
#
#
# evenno1= [2,4,6]
# anotherevenno1 = list(evenno1)
# print(anotherevenno1 is evenno1)
# print(anotherevenno1 == evenno1)
# anotherevenno.sort(reverse=True)
# #below will print different ,since they are 2 different list
# print(evenno1)
even1= [2,4,6,8,10]
odd1= [1,3,5,7,9]
allnos = [even1,odd1]
print(allnos)
for numberset in allnos:
print(numberset)
for val in numberset:
print(val)
Sunday, 4 February 2018
Started Python and Continued - Day1
# for i in [10]:
# print ("now the value of i is {}".format(i))
# i = 0
# while i <10:
# print ("now the value of i is {}".format(i))
# i = i+1
#
# #good example for while loop is below
# exitoptions = ["east","northeast"]
# youroption = ""
# while youroption not in exitoptions:
# youroption = input("please enter your exit direction \n")
# if youroption =="quit":
# print("game over")
# break
# else:
# print("aren't you glad that you are out from there")
# Guess number game using if
# import random
# highest = 10
# random = random.randint(1,highest)
# # print(random)
# answer = 1
# answer = int(input ("Guess any number between 1 and {} \n".format(highest)))
# if (answer==random):
# print("you guessed it corectly \n")
# elif (answer>random):
# answer = int(input("guess lesser no \n"))
# if (answer==random):
# print("you guessed it corectly \n")
# elif (answer<random):
# answer = int(input("guess higher no \n"))
# if (answer==random):
# print("you guessed it corectly \n")
# Guess number game using whileloop
import random
# highest = 10
# random = random.randint(1,highest)
# print(random)
# guessallowed = 5
# guesstime = 1
# guessedno = int(input("guess a no between 1 and {}\n".format(highest)))
# while guesstime <guessallowed and random!=guessedno:
# guessedno = int(input("guess one more time\n"))
# guesstime = guesstime + 1
# if (random == guessedno):
# print("you guessed it at {}- guess".format(guesstime))
# else:
# print("sorry")
Subscribe to:
Posts (Atom)