Monday, December 16, 2019

C program solved practical slips of F.Y.B.Sc. Computer Science Laboratory Course I & II

C program solved practical slips of F.Y.B.Sc. Computer Science Laboratory Course I & II:

C solved practical slips of F.Y.B.Sc. Computer Science

C solved practical slips of F.Y.B.Sc. Computer Science:

Study testing conditions: if, elif, else in python

Testing conditions: if, elif, else :


In Python, we have the if, elif and the else statements for decision making to execute a certain line of codes if a condition is satisfied,and a different set of code in case it is not.


This is the simplest example of a conditional statement. The syntax is:

if(condition):

      indented Statement Block


The block of lines indented the same amount after the colon (:) will be executed whenever the condition is TRUE.


For example, lets say you are recording the score for a certain course.
The total score is 100, with 50 points for the theoretical work and 50 for practical.
You want to display an error message or warning if the score exceeds 100.

score_theory = 40
score_practical = 45
if(score_theory + score_practical > 100):
    print("Invalid score. Please check the input.")
The colon (:) is important because it separates the condition from the statements to be executed after the evaluation of the condition.


The above if statement checks for the 'if' condition and determines the statement
(40 + 45 = 85) > 100 to be FALSE and thus, will not print the warning.
Lets make the statement FALSE and see what happens:

score_theory = 50
score_practical = 55
if(score_theory + score_practical >= 100):
    print("Invalid score. Please check the input.")

Output:
Invalid score. Please check the input.




if-else Statement

 The syntax for the if-else statement is:

if(condition):

      Indented statement block for when condition is TRUE

else:

      Indented statement block for when condition is FALSE


example:
score_theory = 40
score_practical = 45
if(score_theory + score_practical > 100):
    print("Please check the input. Score exceeds total possible score.")
else:
    print("Score validated. Your total is: ", score_theory + score_practical)




Output:

Score validated. Your total is:  85



Multiple tests: if-elif-else Statement:


The syntax followed by the if-else-if statement is:

if(Condition1):
      Indented statement block for Condition1
elif(Condition2):
       Indented statement block for Condition2
else:
       Alternate statement block if all condition check above fails



Example:
coursework = "English"
score_theory = 53
score_practical = 35

if(coursework == "Science" or coursework == "science"):
    if(score_theory > 50):
        print("Please check the input score for 'Science: Theory'.")
    elif(score_practical > 50):
            print("Please check the input score for 'Science: Practical'.") 
    else:
        print("Score validated for Science. Your total is: ",score_theory + score_practical)           
elif(coursework == "English" or coursework == "english"):
    if(score_theory > 60):
        print("Please check the input score for 'English: Theory'.")
    elif(score_practical > 40):
            print("Please check the input score for 'English: Practical'.") 
    else:
        print("Score validated for English. Your total is: ",score_theory + score_practical)
else: print("Coursework not recognized. Please enter score for either Science or English.")

Output:
Score validated for English. Your total is:  88

Variables and objects in python

Learn variables and objects in python:

In Python, values are stored in objects.
If we do
d = 10.0
a new object d is created. As we have given it a floating point value (10.0) the object is of type floating point.
If we had defined d = 10, d would have been an integer object.
In other programming languages, values are stored in variables.
This is not exactly the same as an object, as an object has "methods", that means functions that belong to the object.

There are many object types in Python.

The most important to begin with are:

Object type: Integer
Type class name: int
Description: Signed integer, 32 bit
Example: a = 5

Object type: Float
Type class name: float
Description: Double precision floating point number, 64 bit
Example:b = 3.14

Object type: Complex
Type class name: complex
Description: Complex number
Example: c = 3 + 5j
  c= complex(3,5)


Object type: Character
Type class name: chr
Description:  Single byte character
Example: d = chr(65)
d = 'A'
d = "A"

Object type: String
Type class name: str
Description: List of characters, text string
Example: e = 'LTAM'
e = "LTAM"

A python program to ask the user for his name and greets him

Write a python program to ask the user for his name and greets him:

Answer:
s = raw_input("What is your name?")
print "HELLO ", s

Output:
What is your name?Tom
HELLO Tom

A python program to draw hexagon using Turtle Programming

Write a python program to draw hexagon using Turtle Programming :

Answer:

import turtle 
polygon = turtle.Turtle()
 
num_sides = 6
side_length = 70
angle = 360.0 / num_sides 
 
for i in range(num_sides):
    polygon.forward(side_length)
    polygon.right(angle)
     
turtle.done()

A python program to draw star using Turtle Programming

Write a python program to draw star using Turtle Programming :


Answer:

import turtle 
 
star = turtle.Turtle()
 
for i in range(50):
    star.forward(50)
    star.right(144)
     
turtle.done()

A python program to draw square using Turtle Programming

Write a python program to draw square using Turtle Programming

Answer:

import turtle 
skk = turtle.Turtle()
 
for i in range(4):
    skk.forward(50)
    skk.right(90)
     
turtle.done() 

Python program to demonstrate all stack operations using a doubly linked list

Write a python program to demonstrate all stack operations using a doubly linked list  :


Answer:

class Node:
 
    def __init__(self, data):
        self.data = data # Assign data
        self.next = None # Initialize next as null
        self.prev = None # Initialize prev as null       
         

class Stack:
    # Function to initialize head 
    def __init__(self):
        self.head = None

    def push(self, data):
 
        if self.head is None:
            self.head = Node(data)
        else:
            new_node = Node(data)
            self.head.prev = new_node
            new_node.next = self.head
            new_node.prev = None
            self.head = new_node
             

    def pop(self):
 
        if self.head is None:
            return None
        else:
            temp = self.head.data
            self.head = self.head.next
            self.head.prev = None
            return temp
 
 

    def top(self):
 
        return self.head.data
 
 

    def size(self):
 
        temp = self.head
        count = 0
        while temp is not None:
            count = count + 1
            temp = temp.next
        return count
           
    def isEmpty(self):
 
        if self.head is None:
           return True
        else:
           return False
             

    def printstack(self):
         
        print("stack elements are:")
        temp = self.head
        while temp is not None:
            print(temp.data, end ="->")
            temp = temp.next         
         
 
       
if __name__=='__main__': 
 

  stack = Stack()
 
  print("Stack operations using Doubly LinkedList")
  stack.push(4)
 

  stack.push(5)
 

  stack.push(6)
 

  stack.push(7)
 
  stack.printstack()
 

  print("\nTop element is ", stack.top())
 

  print("Size of the stack is ", stack.size())
 

  stack.pop()
 

  stack.pop()
   


  stack.printstack()
   

  print("\nstack is empty:", stack.isEmpty())




Output:
Stack operations using Doubly LinkedList
stack elements are:
7->6->5->4->
Top element is  7
Size of the stack is  4
stack elements are:
5->4->
stack is empty: False

Python program to create a list of tuples from given list having number and its cube in each tuple

Write a python program to create a list of tuples from given list having number and its cube in each tuple:


Answer:

list1 = [1, 2, 7, 8]
res = [(val, pow(val, 3)) for val in list1]
print(res)

Output:
[(1, 1), (2, 8), (7, 343), (8, 512)] 

Featured posts

Mongolia

 Mongolia! Mongolia is a vast and sparsely populated country in East Asia, known for its stunning natural beauty, rich history, and unique c...

Popular posts