# comment in separate line x = x * 5 x = x * 5 # multiply by 5; comment in same line # this function is going to divide two variables # the second value should not be zero # if it is zero, error is thrown def divide(first, second):
# valid names numOfBoxes = 7 _num_of_boxes = 10 # this is a different variable than numOfBoxes _NUM_OF_BOXES = 15 # a different variable as names are case sensitive ownerName = "Karthik"ownerName2 = "Charan"# different, valid variable # invalid names 2ownerName = "David"# cannot start with number. # Only letter or underscore in the beginning owner-name = "Ram"# no hypen owner name = "Krish"# no space allowed # only alpha numeric and underscore
# different values assigned to many variables length, width, depth = 5, 8, 7 print(length) print(width) print(depth) # same value assigned to many variables length = width = depth = 5 print(length) print(width) print(depth)
a = 10 b = 6 print (a + b) # addition print (a - b) # subtraction print (a * b) # multiplication print (a / b) # division print (a % b) # modulus print (a ** b) # exponentiation print (a // b) # floor division
a = 7 # assign value to a b = a # assign value of a into b c = a + b -2 # calculate an expression and place result into c b += 2 # equivalent to b = b + 2 b -= 2 # equivalent to b = b - 2 b *= 2 # equivalent to b = b * 2 b /= 2 # equivalent to b = b / 2 b %= 2 # equivalent to b = b % 2 b //= 2 # equivalent to b = b // 2 b **= 2 # equivalent to b = b ** 2 b &= 2 # equivalent to b = b & 2 b |= 2 # equivalent to b = b | 2 b ^= 2 # equivalent to b = b ^ 2 b >>= 2 # equivalent to b = b >> 2 b <<= 2 # equivalent to b = b << 2
最后五个运算符将在下面的后面部分中说明。
16.比较运算符
比较两个值,结果是布尔值True或False。在if和loop语句中使用以进行决策。
a = 3 b = 7 print(a == b) # true if a and b are equal print(a != b) # true if a and b are not equal print(a > b) # true if a is greater than b print(a < b) # true if a is less than b print(a >= b) # true if a is greater than or equal to b print(a <= b) # true if a is less than or equal to b
a = 3 b = 7 if(a < b): print("first number is less than second one"); else: print("first number is greater than or equal to second one")
17.逻辑运算符
可以使用逻辑运算符组合两个或多个比较操作。这些逻辑运算符返回布尔值。
a = 5 b = 8 # True if both conditions are true print(a > 3 and a < 7) # True if one condition is true print(a > 6 or b < 7) # True if given condition is false (inverse of given condition) print(not(a > 3))
18.身份运算符
身份运算符比较两个对象是否相同。他们需要指向相同的位置。
a = ["hello", "world"] b = ["hello", "world"] c = a # prints true as both are same element print(a is c) # prints false as they are two diffent values # content may be same, but value locations are different print(a is b) # comparing the values gives true print(a == b) # not negates the comparison result # if two variables are not same, then result is true print(a is not c) print(a is not b)
19.会员经营者
检查给定列表中是否存在元素。
a = ["hello", "world"] # checks if given element is present in the a list print("world" in a) # checks if given element is not present in the a list print("world" not in a)
20.按位运算符
在处理二进制数时,我们需要按位运算符来操作它们。二进制数是零和一,它们被称为位。
a = 1 # binary equivalent of 1 is 1 b = 2 # binary equivalent of 2 is 10 # In case of AND(&) operation, if both bits are 1, set result to one # in our case, two corresponding bits are not 1 # so, corresponding result is 0 print(a & b) # OR operation (|), gives 1 if either operands are 1 # The reult of 0 and 1 is 1, 1 and 0 is 1 # hence, a | b gives binary 11 or decimal equivalent of 3 print(a | b) # XOR (^) returns 1 if only one of the operands is 1 # 1 ^ 1 = 0, 0 ^ 0 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1 # considering a and b, they have only ones in each bit position print(a ^ b) # NOT operation negates the bit value # NOT 0 = 1, NOT 1 = 0 # while negating b, all preceeding zeros are turned to one # this leads to a negative number print(~ b) # zero fill left shift (<# zeros are filled from right, bits are shifted to left # left most bits fall off # in this example, we shift the bits of b twice # 10 (b) shifted twice is 1000 (which is decimal 8) print(b << 2) # signed right shift (>>) # shift the bits to the right, copy leftmost bits to the right # right most bits fall off # when we shift b (10) once to the right, it becomes 1 print(b >> 1)
# a lambda function with the name growth is created # it takes one argument and increase it by 2 growth = lambda givenLength : givenLength + 2 print(growth(25)) # prints 27 Let us take another example with many parameters:# lambda with the name area is created # two parameters are passed, who area is calculated area = lambda length, breadth: length * breadthprint(area(4, 7)) # prints 28
当嵌套在另一个函数中时,lambda很有用。函数成为创建函数的模板。
# the growth lambda we wrote earlier def growth(n): return lambda a : a + n # create a function that would increase by 2 strechTwo = growth(2) # create a function that would increase by 3 strechThree = growth(3) print(strechTwo(7)) print(strechThree(7))
# create a class with "class" keyword class Fruit: # a property, "name" is created # the property is assigned with the value "mango" name="mango" # let us create an object, "oneFruit" using the above class oneFruit = Fruit() # the property of the object "oneFruit" is accessed like this print(oneFruit.name)
# create a class with "class" keyword class Fruit: # define the init function def __init__(self, name, color): self.name = name self.color = color # let us create an object, "oneFruit" using the above class # values are passed to the class oneFruit = Fruit("mango", "yellow") # the property of the object "oneFruit" is accessed like this print(oneFruit.name) print(oneFruit.color)
# create a class with "class" keyword class Fruit: # define the init function def __init__(self, name, color): self.name = name self.color = color def makeJuice(self): print("Made " + self.name + " juice. It will be in " + self.color + " color.") # let us create an object, "oneFruit" using the above class # values are passed to the class oneFruit = Fruit("mango", "yellow") # invode object's method oneFruit.makeJuice() The property of the object can be modified like this:oneFruit.color = "red"
city = input("Where do you live? ") print("city you live is : " + city)
34.字符串格式
字符串变量可以使用format()方法进行格式化。首先创建模板字符串,并在格式化阶段附加值。
tStr = "The fruit {} is {} color." print(tStr.format("mango", "yellow")) print(tStr.format("apple", "red"))
您可以使用索引引用这些值。索引从零开始。
tStr = "The fruit {0} is {1} color. {0} is good for health" print(tStr.format("mango", "yellow")) print(tStr.format("apple", "red"))
类似于索引引用,我们可以使用参数名称。
tStr = "The fruit {name} is {color} color. {name} is good for health"print(tStr.format(name = "mango", color = "yellow")) print(tStr.format(name = "apple", color = "red"))