Posts

Showing posts with the label Computer Science

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.

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)