Posts

Showing posts from July, 2021

Program to print odd and even numbers in python

 #print even numbers in a given sequence numbers = [10,20,30,40,50,60,70,80,90,100] for num in numbers:   if num %2 == 0:     print(num, "is an even number" ) Try running code in python and check the output.

Program to create a four function calculator

 num1 = float(input("Enter first number:")) num2 = float(input("Enter second number:")) a = num1 + num2 p = num1 * num2 s = num1 - num2 d = num1 / num2 print("sum of num1 and num2 is", a) print("product of num1 and num2 is", p) print("subtraction of num1 and num2 is" , s) print("division of num1 and num2 is" , d) ☝☺

Data types in Python

 Data Types Every value belongs to a specific data type in Python.  Data type identifies the type of data values a variable  can hold and the operations that can be performed on  that data. Figure 5.6 enlists the data types available  in Python. Number Number data type stores numerical values only. It is  further classified into three different types: int, float and complex. Boolean data type (bool) is a subtype of integer. It  is a unique data type, consisting of two constants, True and False. Boolean True value is non-zero, non-null  and non-empty. Boolean False is the value zero.

Everything is an object

 Python treats every value or data item whether numeric,  string, or other type (discussed in the next section) as  an object in the sense that it can be assigned to some  variable or can be passed to a function as an argument. Every object in Python is assigned a unique identity  (ID) which remains the same for the lifetime of that object.  This ID is akin to the memory address of the object. The  function id() returns the identity of an object.

How to add comments in python ?

 Comments Comments are used to add a remark or a note in the  source code. Comments are not executed by interpreter.They are added with the purpose of making the source  code easier for humans to understand. They are used  primarily to document the meaning and purpose of  source code and its input and output requirements,  so that we can remember later how it functions and  how to use it. For large and complex software, it may  require programmers to work in teams and sometimes,  a program written by one programmer is required to be  used or maintained by another programmer. In such  situations, documentations in the form of comments  are needed to understand the working of the program. Write a Python program to find the sum of  two numbers. #Program 5-4 #To find the sum of two numbers num1 = 10 num2 = 20 result = num1 + num2 print(result)  Output: 30

How to create a list in Python

 List  List is a sequence of items separated by commas and  the items are enclosed in square brackets [ ]. #To create a list list1 = [5,3.4,"New Delhi","20C", 45] #print the elements of list1 print(list1)

How to create a set in python

Set Set is an unordered collection of items separated by commas  and the items are enclosed in curly brackets { }. A set is  similar to list, except that it cannot have duplicate entries.  Once created, elements of a set cannot be changed. Run this code in python or online python compiler to create a set #Create set set1 = {10,20,30,40,50} #print the elements of set 1 print (set1)