Posts

Showing posts from January, 2024

According To Data Science Topic 16 Sequence sum Task

  # sequence sum # 1/1! + 2/2! + 3/3! + ... + n/n! n = int(input('enter n')) result = 0 fact = 1 for i in range(1,n+1):   fact = fact * i   result = result + i/fact   print(result) Result: enter n1 1.0 enter n2 1.0 2.0 enter n3 1.0 2.0 2.5 enter n4 1.0 2.0 2.5 2.6666666666666665 enter n5 1.0 2.0 2.5 2.6666666666666665 2.708333333333333   

According to Data Science Topic-15 For loop and examples

 # for loop demo for i in range(1,11):  print(i) Result: 1 2 3 4 5 6 7 8 9 10 for i in range(1,11,3):   print(i) Result: 1 4 7 10 for i in range(10,0,-1):   print(i) Result: 10 9 8 7 6 5 4 3 2 1 i = 'delhi' for i in 'delhi':   print(i) Result: d e l h i for i in [1,2,3,4,5]:   print(i) for i in (1,2,3,4,5):   print(i) for i in {1,2,3,4,5}:   print(i) Result: 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 # Program - The current population of the town is 10000. The population of the town is increasing at the rate of 10% per year. You have to write a program to find out the population at the end of each of the last 10 years. curr_pop = 10000 for i in range(10,0,-1):   print(i,curr_pop)   curr_pop = curr_pop - (curr_pop/1.1) Result: 10 10000 9 909.0909090909099 8 82.6446280991737 7 7.513148009015794 6 0.6830134553650726 5 0.062092132305915704 4 0.005644739300537799 3 0.0005131581182307096 2 4.665073802097362e-05 1 4.240976183724878e...

According to Data Science Topic-14 While loop examples

  # While loop example -> program to print the table # Program -> Sum of all digits of a given number # Program -> Keep accepting numbers from users till he/she enters a 0 and then find the average number = int(input('enter the number')) i = 1 while i <= 11:   print(number, 'x', i, '=', number * i)   i = i + 1 Result: enter the number11 11 x 1 = 11 11 x 2 = 22 11 x 3 = 33 11 x 4 = 44 11 x 5 = 55 11 x 6 = 66 11 x 7 = 77 11 x 8 = 88 11 x 9 = 99 11 x 10 = 110 # while loop with else11 x = 1 while x < 3:   print(x)   x += 1 else:   print('limit crossed') Result:1 2 limit crossed # guessing game # generate a random integer between 1 and 100 import random jackpot = random.randint(1, 100) guess = int(input('Guess the number: ')) counter = 1 while guess != jackpot:     if guess < jackpot:         print('Incorrect! Guess higher.')     else:         print('Incorrect! Guess lower.') ...

According to Data Science Topic-13 Modules

 # Modules in Python # 1-math # 2-keywords # 3-random # 4-datetime # math import math  print(math.factorial(5))  print(math.floor(6.8))  print(math.sqrt(196)) Result: 120 6 14.0 # keyword import keyword print(keyword.kwlist) Result: ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] # random import random print(random.randint(1,100)) Result:48 # datetime import datetime print(datetime.datetime.now()) Result: 2024-01-13 15:42:37.384551 help('modules')

According to Data Science Topic-12 If else program examples

  # email- abhayprasad.36480@gmail.com # password-1234 email = input('Enter your email: ') password = input('Enter your password: ') if email == 'abhayprasad.36480@gmail.com' and password == '1234':   print('Welcome') elif email == 'abhayprasad.36480@gmail.com' and password != '1234':   # tell the user   print('Incorrect password')   password = input('enter password again')   if password == '1234':     print('Welcome,finally!')   else:     print('beta tumse na ho payega!') else:   print('Not correct') Result: Enter your email: abcd Enter your password: 7689 Not correct Enter your email: abhayprasad.36480@gmail.com Enter your password: 1234 Welcome Enter your email: abhayprasad.36480@gmail.com Enter your password: 4567 Incorrect password enter password again9000 beta tumse na ho payega! Enter your email: abhayprasad.36480@gmail.com Enter your password: 4567 Incorrect password enter pa...

According to Data Science Topic-11 one program for Operators

 number = int(input("Enter a number: ")) print(number) # 345%10 -> 5 a = number%10 # 345%10 -> 34 number = number//10 # 34%10 -> 4 b = number % 10 number = number//10 # 3 % 10 -> 3 c = number % 10 print(a+b+c) Result:  Enter a number: 345 345 12 Enter a number: 666 666 18

According to Data Science Topic-10 Operators

 # Arithmetric Operators print(5+6) print(5-6) print(5*6) print(5/2) print(5//2) print(5%2) print(5**2) Result: 11 -1 30 2.5 2 1 25 # Relational Operators print(4>5) print(4<5) print(4>=4) print(4<=4) print(4==4) print(4!=4) Result: False True True True True False # Logical Operators print(1 and 0) # (00 and 01) print(1 or 0) print(not 1) Result: 0 1 False # Bitwise Operators # bitwise and print(2 & 3) # bitwise or print(2 | 3) # bitwise xor print(2 ^ 3) print(~3) print(4 >> 2) print(5 << 2) Result: 2 3 1 -4 1 20 # Assignment Operators # =  # a = 2 a = 2  # a = a % 2 # a = a + 2 # a %= 2 a += 2 # a++ ++a(This is not used in python) print(a) Result: 4 # Membership Operators # in/not in print('D' not in 'Delhi') print(1 in [2,3,4,5,6]) Result: False False

According to Data Science Topic-9 Literals

  a = 0 b1010 #Binary Literals b = 100 #Decimal Literal c = 0 o310 #Octal Literal d = 0x 12c #Hexadecimal Literal #Float Literal float_1 = 10.5 float_2 = 1.5e2 # 1.5 * 10^2 float_3 = 1.5e-3 # 1.5 * 10^-3 #Complex Literal x = 3.14j print (a, b, c, d) print (float_1, float_2,float_3) print (x, x.imag, x.real) # binary x = 3.14j print (x.imag) 3.14 string = 'This is Python' strings = "This is Python" char = "C" multiline_str = """This is a multiline string with more than one line code.""" unicode = u "\U0001f600\U0001F606\U0001F923" raw_str = r "raw \n string" print (string) print (strings) print (char) print (multiline_str) print ( unicode ) print (raw_str) This is Python This is Python C This is a multiline string with more than one line code. 😀😆🤣 raw \n string k = None a = 5 b = 6 print ( 'Program exe' ) Program exe a = 0b1010 # Binary Literals print(a) # binary c = 0o310 # Octal Literals p...

According to Data Science Topic-8 Type conversion

  # Explicit # str -> int int('4') 4 type(int('4')) int type(int('4.5') 4 int(4+5j) error # int to str str(5) 5 # float float(4) 4.0 fnum = input('Enter first number') snum = input('Enter second number') print(type(fnum), type(snum)) print(fnum,snum)     # add 2 variables result = int(fnum) + int(snum) print(result) print(type(fnum)) Output: Enter first number56 Enter second number67 <class 'str'> <class 'str'> 56 67 123 when we want to know the data type of a function then we will know the result is a string because the original data type has not changed in Python type conversion. fnum = int(input('Enter first number')) snum = int(input('Enter second number')) print(type(fnum), type(snum)) print(fnum,snum)     # add 2 variables result = int(fnum) + int(snum) print(result) Output: Enter first number56 Enter second number67 <class 'int'> 123

According to Data science topic-7 User input

 # input('Enter email') # take input from users and store them in a variable fnum = input('Enter first number') snum = input('Enter second number') print(type(fnum), type(snum)) print(fnum,snum) # add 2 variables result = fnum + snum # print the result Output: Enter first number45 Enter second number56 45 56 Enter first number56 Enter second number67 56 67 5667

According to Data Science Topic-6 Identifiers

 # identifiers # you can't start with a digit name1 = 'Abhay' print(name1) Abhay # You can use special chars -> _ first_name = 'Abhay' print(first_name) Abhay _ = 'Abhay' print(_) Abhay # identifiers can not be keyword

According to Data Science Topic -5 Keywords

 # Keywords and A logical operator as To create an alias assert For debugging break To break out of a loop class To define a class continue To continue to the next iteration of a loop def To define a function del To delete an object elif Used in conditional statements, same as else if else Used in conditional statements except Used with exceptions, what to do when an exception occurs False Boolean value, result of comparison operations finally Used with exceptions, a block of code that will be executed no matter if there is an exception or not for To create a for loop from To import specific parts of a module global To declare a global variable if To make a conditional statement import To import a module in To check if a value is present in a list, tuple, etc. is To test if two variables are equal lambda To create an anonymous function None Represents a null value nonlocal To declare a non-local variable not A logical operator or A logical operator pass A null statement, a statemen...

According to Data Science Topic-4 Comments

 # this is a comment # second line a = 5 # like this b = 7 # this is a second comment print(a+b) 12 comment is very important for programming when you are working in a team.

According to Data Science Topic-3 Variables

 name = 'Abhay" print(name) Abhay a = 5 b = 6 print(a+b) 11 # Dynamic Typing a = 5 # Static Typing int a = 5 # Dynamic Binding a = 5 print(a) a = 'Abhay' print(a) 5 Abhay #Static Binding(c,c++,java) int a = 5 a = 1 b= 2 c = 3 print(a,b,c) 1 2 3 a,b,c = 1,2,3 print(a,b,c) 1 2 3 a=b=c=5 print(a,b,c) 5 5 5

According to Data Science Python Topic-2 Data Types

  # Integer print(8) 8 # 1*10^308 print(1e308) 1e+308 #Decimal/Float print(8.55) print(1.7e308) 8.55 1.7e+308 # Boolean(Boolean is not a text) print(True) print(False) True False #Text/String print('Hello World') Hello World #Complex print(5+6j) 5+6j # List->C->Array print([1,2,3,4,5]) [1,2,3,4,5] # Tuple print((1,2,3,4,5)) (1,2,3,4,5) # Sets print({1,2,3,4,5}) {1,2,3,4,5} # Dictionary print({'name': 'Nitish',' gender': 'Male', 'weight':70}) {'name': 'Nitish', ' gender': 'Male', 'weight': 70} #type x = 20 print(type(x)) <class , int'>

According to Data Science Python Topic-1

 print("Hello World") Hello World print(7) 7 print('true') true print('false') false print('Hello',1,4.5,True) Hello 1 4.5 True print('Hello',1,4.5,True,sep='/') Hello/1/4.5/True print('Hello' ,end='-') print('world!') Hello-World print('Hello' ,end='/n') print('World') Hello/nWorld