Home - nil41/GCP_services GitHub Wiki

MY PYTHON STUDY:-

print "Welcome to Python!" # hash is used for single line comment

my_variable = 10 print my_variable // it will print 10

my_int = 10 my_float = 10.5 my_bool = True

print my_int // it will print 10, 10.5, True respectivly print my_float
print my_bool

my_int = 7 my_int = 10

print my_int // it will print 10 (reassigned)

Whitespace:- whitespace is used to structure code. Whitespace is important, so you have to be careful with how you use it. eg1-

def spam(): eggs = 12 return eggs

print spam() // this eg gives error

eg2-

def spam(): eggs = 12 // just give 2 blank space return eggs

print spam() // this will print 12

""" hi this is used for multi line comment """

adition = 72 + 23 // we can also use -,*,/,% print adition

eggs = 2 ** 3 // used for exponential (2^3)

name = "Ryan" age = "19" food = "cheese" // all are treat as string use "" or ''

Escaping characters:- There are some characters that cause problems. For example:

'There's a snake in my boot!' solution is, 'There's a snake in my boot!'

Access by Index- Each character in a string is assigned a number. This number is called the index. index of first character is 0.

c = "cats"[0]
n = "Ryan"[3]

String methods:-len(), lower(), upper(), str()

parrot = "Norwegian Blue" print len(parrot) // it will print 14

parrot = "Norwegian Blue" print parrot.lower() // it will print norwegian blue (invers for upper())

pi = 3.14 print str(pi) // it will print 3.14 but treat as string

print "Spam " + "and " + "eggs" // it will print Spam and eggs with spaces in between them

print "I have " + str(2) + " coconuts!" // here 2 is treat as string, This will print I have 2 coconuts!

string_1 = "Camelot" string_2 = "place"

print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2) // this will print Let's not go to Camelot. 'Tis a silly place.

name = raw_input("What is your name? ") quest = raw_input("What is your quest? ") color = raw_input("What is your favorite color? ")

print "Ah, so your name is %s, your quest is%s, "
"and your favorite color is %s." % (name, quest, color) // this will print string with name, quest, color inputs which enter by user.
##################################################################################### Conditonal statements:-

if 8 > 9: print "I don't printed!" else: print "I get printed!"

answer = "'Tis but a scratch!"

def black_knight(): if answer == "'Tis but a scratch!": return True else:
return False

def greater_less_equal_5(answer): if answer > 5: return 1 elif answer < 5:
return -1 else: return 0

print greater_less_equal_5(4) print greater_less_equal_5(5) print greater_less_equal_5(6) // it will print -1 0 1

we can also use if 5>4 and 4 > 3:

original = raw_input("Enter a word:") if len(original) > 0: print original else: print "empty" // to check wheather input is empty or not.

original = raw_input("Enter a word:") if len(original) > 0 and original.isalpha(): // to check string is non empty as well it is string only i.e. non-aplhabets (e.g. hj41) print original else: print "empty"