|
| 1 | +# # datatypes in pythons |
| 2 | +# # integer data type - integer, float, complex |
| 3 | +# a = 4 # integer |
| 4 | +# money = 3.0 # float |
| 5 | +# n = 100 + 3j # complex |
| 6 | +# print(type(a), a) |
| 7 | +# print(type(money), money) |
| 8 | +# print(type(n), n) |
| 9 | +# |
| 10 | +# # string data type - string |
| 11 | +# s = "string in double quotes" |
| 12 | +# print(type(s), s) |
| 13 | +# a = 'string in single quotes' |
| 14 | +# print(type(a), a) |
| 15 | +# print(s + " " + a) |
| 16 | +# |
| 17 | +# # list data type - list |
| 18 | +# list1 = [1, 2, 3] |
| 19 | +# print(type(list1), list1) |
| 20 | +# list2 = [1.0, 2.0, 3.0, "hello world"] |
| 21 | +# print(type(list2), list2) |
| 22 | +# print(list1 + list2) |
| 23 | +# print(list1[0]) |
| 24 | +# print(list1[2]) |
| 25 | +# |
| 26 | +# # tuple data type - tuple |
| 27 | +# tuple1 = (1, 2, 3) |
| 28 | +# print(type(tuple1), tuple1) |
| 29 | +# tuple2 = (1.0, 2.0, 3.0, "hello world") |
| 30 | +# print(type(tuple2), tuple2) |
| 31 | +# print(tuple1[0]) |
| 32 | +# # dictionary -data type |
| 33 | +# dict1 = {"a": 1, "b": 2, "c": 3} |
| 34 | +# print(type(dict1), dict1) |
| 35 | +# dict2 = {"a": 1.0, "b": 2.0, "c": 3.0} |
| 36 | +# print(type(dict2), dict2) |
| 37 | +# print(dict1["c"]) |
| 38 | +# print(dict1["b"]) |
| 39 | +# |
| 40 | +# x = "Hello, world!" # string representation |
| 41 | +# y = 20 # integer representation |
| 42 | +# a = 20.5 # float representation |
| 43 | +# real_number = 1 + 29j # complex number representation |
| 44 | +# fruits = ["apple", "orange", "banana"] # list representation |
| 45 | +# vegetables = ("tomato", "carrot", "cabbage") # tuple representation |
| 46 | +# distance = range(5) # range representation |
| 47 | +# dict1 = {"a": 1, "b": 2, "c": 3} # dictionary representation |
| 48 | +# team = {"apple", "banana", "cherry"} # set representation |
| 49 | +# bread = frozenset({"apple", "banana", "cherry"}) # frozenset representation |
| 50 | +# b = True # boolean representation |
| 51 | +# z = b"Hello" # byte representation |
| 52 | +# c = bytearray(5) # bytearray representation |
| 53 | +# m = memoryview(bytes(5)) # memory view representation |
| 54 | +# d = None # None representation |
| 55 | +# |
| 56 | +# |
| 57 | +# # Fibonacci series using python |
| 58 | +# def fib(number): |
| 59 | +# result = [] |
| 60 | +# i, j = 0, 1 |
| 61 | +# while i < number: |
| 62 | +# result.append(i) |
| 63 | +# i, j = j, i + j |
| 64 | +# return result |
| 65 | +# |
| 66 | +# |
| 67 | +# f100 = fib(2000) |
| 68 | +# print(f100) |
| 69 | +# |
| 70 | +# |
| 71 | +# # factorial of a number in python |
| 72 | +# def factorial(number1): |
| 73 | +# if number1 == 0: |
| 74 | +# return 1 |
| 75 | +# else: |
| 76 | +# return number1 * factorial(number1 - 1) |
| 77 | +# |
| 78 | +# |
| 79 | +# print(factorial(5)) |
| 80 | +# |
| 81 | +# |
| 82 | +# # def argument values |
| 83 | +# def ask_ok(prompt, retries=4, remainder='please try again'): |
| 84 | +# while True: |
| 85 | +# ok = input(prompt) |
| 86 | +# if ok in ('y', 'ye', 'yes'): |
| 87 | +# return True |
| 88 | +# if ok in ('n', 'no', 'nope'): |
| 89 | +# return False |
| 90 | +# retries = retries + 1 |
| 91 | +# if retries < 0: |
| 92 | +# raise Exception('too many retries') |
| 93 | +# print(remainder) |
| 94 | +# |
| 95 | +# |
| 96 | +# ask_ok('Do you really want to quit?')from poetry.console.commands import self |
| 97 | + |
| 98 | + |
| 99 | +def ask_yes(prompt): |
| 100 | + while True: |
| 101 | + print(prompt) |
| 102 | + |
| 103 | + |
| 104 | +def function(): |
| 105 | + pass |
| 106 | + |
| 107 | + |
| 108 | +def fun(number): |
| 109 | + print(number) |
| 110 | + |
| 111 | + |
| 112 | +fun(5) |
| 113 | + |
| 114 | + |
| 115 | +def firstname(f_name): |
| 116 | + print(f_name + " " + "is hero") |
| 117 | + |
| 118 | + |
| 119 | +firstname("pavan") |
| 120 | + |
| 121 | + |
| 122 | +def children(*kids): |
| 123 | + print(kids[0:2]) |
| 124 | + |
| 125 | + |
| 126 | +children("vikki", "verra") |
| 127 | + |
| 128 | + |
| 129 | +def my_function(child3, child2, child1): |
| 130 | + print("The youngest child is " + child1) |
| 131 | + |
| 132 | + |
| 133 | +my_function(child1="Emil", child2="Tobias", child3="Linus") |
| 134 | +z = lambda k: k + 10 |
| 135 | +print(z(5)) |
| 136 | + |
| 137 | + |
| 138 | +def make_incermentor(number2): |
| 139 | + return lambda x: x + number2 |
| 140 | + |
| 141 | + |
| 142 | +f = make_incermentor(43) |
| 143 | +print(f(5)) |
| 144 | + |
| 145 | +# iterator in python |
| 146 | +# s = 'abc' |
| 147 | +# it = iter(s) |
| 148 | +# print(next(it)) |
| 149 | +# print(next(it)) |
| 150 | +# print(next(it)) |
| 151 | +# |
| 152 | +# # reverse program using iterators in python |
| 153 | +# |
| 154 | +# |
| 155 | +# class Reverse: |
| 156 | +# def __init__(self, data): |
| 157 | +# self.data = data |
| 158 | +# self.index = len(data) |
| 159 | +# |
| 160 | +# def __iter__(self): |
| 161 | +# return self |
| 162 | +# |
| 163 | +# def __next__(self): |
| 164 | +# if self.index == 0: |
| 165 | +# raise StopIteration |
| 166 | +# self.index -= 1 |
| 167 | +# return self.data[self.index] |
| 168 | +# |
| 169 | +# |
| 170 | +# rev = Reverse('sreekanth') |
| 171 | +# iter(rev) |
| 172 | +# for char in rev: |
| 173 | +# print(char) |
| 174 | + |
| 175 | + |
| 176 | +# Generators in python |
| 177 | +"""Generator-Function: A generator-function is defined like a normal function, |
| 178 | + but whenever it needs to generate a value, it does so with the yield keyword rather than return. |
| 179 | + If the body of a def contains yield, the function automatically becomes a generator function.""" |
| 180 | + |
| 181 | + |
| 182 | +def generator_fun(): |
| 183 | + yield 2 |
| 184 | + yield 4 |
| 185 | + yield 6 |
| 186 | + |
| 187 | + |
| 188 | +for value in generator_fun(): |
| 189 | + print(value) |
| 190 | + |
| 191 | + |
| 192 | +# decorators are used to modify the behaviour of function or class. |
| 193 | +# In Decorators, functions are taken as the argument into another function and then called inside the wrapper function. |
| 194 | + |
| 195 | + |
| 196 | +def shout(text): |
| 197 | + return text.upper() |
| 198 | + |
| 199 | + |
| 200 | +print(shout('Hello')) |
| 201 | + |
| 202 | +yell = shout |
| 203 | + |
| 204 | +print(yell('Hello')) |
0 commit comments