August 2017 - Coders PlayGround

Learn to code and change the future!

Tuesday, 29 August 2017

Functions with default parameter in Python3

August 29, 2017


Hello friends, lets learn something more about functions in Python3.

We can set default parameters in the function.

Check the following code:

def simple(num1, num2):
  pass

def simple(num1,num2=5):
  print("Number 1 and Number 2 default")
  print(num1,num2)

def class_a(name, roll_no, branch = "IT", strength_of_class = 90):
  print (name, '\n', roll_no,'\n',branch,'\n',strength_of_class)


simple(10)
simple(10,20)
class_a('Kevin',38)


Output



Read More

Functions with Parameter in Python3

August 29, 2017

Functions with parameter

Hello friends, It is also possible to define functions with a variable number of arguments. 

Let us look at the code:

Here we define two parameters, num1 and num2. Then while calling the function we can pass the value to the function.

def simple_addition(num1, num2):
  answer = num1 + num2
  print ("Num1 is:", num1)
  print ("Num2 is:",num2)
  print ("Addition:", answer)


simple_addition(5,10)
simple_addition(num1 = 10, num2 = 5)
simple_addition(num2 = 5, num1 = 10)
'''
same no of parameters should be passed
'''
 
Output
 
 
 
 
 
 
 
 
Read More

Function in Python3

August 29, 2017

Functions in Python

Hello friends, let us learn about function in Python3. 

We define function using the keyword def

So basically:

def function_name(para1,para2):
   #code

The default value returned is None.
You can check it:


Check out the sample program:

def example():
  print ('Yay! This is my function!')

for x in range(1,6):
  example()







Read More

String in Python3

August 29, 2017
Hello friends, lets learn about the string in Python3.

Concatenation in Python3 can be done using two different ways, 
 a) Using a '+' operator
 b) Using a ',' operator

Sample Program:
 
# This doesn't add space unless we add 
print ("Hello" + "there!")

# This adds space
print ("Hello","there!")

# string and integer
print ("Hi" + "5")

#if only 5 is written then error will be displayed
# we can also write

print ("hi",str(5))

'''
If you don’t want characters prefaced by \ to
be interpreted as special characters, you can use
raw strings by adding an r before the first quote

'''
# check out this will treat (\n)ew as newline
# To avoid that check the second condition
print ('\Desktop\python3\new')
print (r'\Desktop\python3\new')

print ("""
Name Div 
ABC A
Xyz B
""")
 
Output
Read More

Range function in Python3

August 29, 2017
Hello friends, lets learn about range function in Python3.

The range function generates arithmetic progression.

Syntax of range function:
    range (start_num, end_num, step)


For example:

range(0,10,2) 

It will start at 0 and will increment by 2, the last no printed will be 8.

The output will be: 0 2 4 6 8


Check out this program:

print ("Using range function...")
# xrange in Python 2.7
# range built in function

for x in range(1,11):
  print (x)


# prints from 1 --> 10
Output

Read More

If in Python3

August 29, 2017
Hello friends, lets learn about the if structure in Python3.


The 'if' is same as in C, if the condition is true then the code is executed or else it is not executed.

Check out this program:

x = 5
y = 8
z = 5

if x < y:
  print ('x is less than y ')


if z < y > x:
  print ('y is greater than x and greater to z')


if z == y:
  print ('z is equal to y')
Output
Read More

If - Else Structure in Python3

August 29, 2017
Hello friends, lets learn about the if-else structure!

Syntax:

if condition:
    #code
else:
    #code


Check out this program:

x = 5
y = 8

if x > y:
  print ('x is greater than y')
else:
  print ('y is greater than x')
Output
Read More

If elif else Structure in Python

August 29, 2017
Hello friends, lets learn about the if-elif-else structure.

In C we write else if, here we use elif.

else is optional. 


Check out this program:

 
x = 5
y = 10
z = 20

if x > y:
  print ('x is greater than y')
elif x < z:
  print ('x is less than z')
elif 5>2:
  print ('5 is greater than 2')
else:
  print ('if and elif(s) never ran')


Output



 
 
The if elif else is a code together, anyone one of it returns true then we come out of the loop.

As in this case, we see than 5>2 is true but since the statement above it is true, it came out of the loop.

Read More

For Loop in Python3

August 29, 2017

Hello friends, lets learn about for loop in python.
The for loop in python is bit different than C. 

Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.


Check out this program:

list1 = [1,5,4,6,9,8,7,1,5,2,0,1,3]

for eachno in list1:
  print (eachno)

# highlight and tab to indent
# indentation is important in Python


print ("Using range function...")
# xrange in Python 2.7
# range built in function

for x in range(1,11):
  print (x)


# prints from 1 --> 10

 
Output
Read More

While Loop in Python3

August 29, 2017
Hello friends, lets learn about flow control Statements

In this post, I'll explain about while loop 

The syntax of while loop:

counter_var 
while condition:
    # Code to execute
    Increment/Decrement the counter_var


Remember, in Python indentation plays a very important role.
We do not use curly brackets for defining the while loop instead indentation is used to define the code within the while loop.



Check out the following code:


condition = 1

while condition < 10:
  print(condition)
  condition += 1

# Infinte loop
# Ctrl C to break the loop
while True:
  print ("Processing...")


Output
Read More

Math Functions

August 29, 2017

Hello friends, lets learn about the math functions of Python3



import math

a = 2
b = 5

print(a+b)
print(a-b)
print(a/b)
print(a*b)
print(a**b)
print("Value of pi: ", math.pi)
print("Ceil function",math.ceil(5.4))
print("Floor function",math.floor(5.4))
print("Absolute function",math.fabs(-5))
print("Factorial function",math.factorial(5))
print("Greates common divisor function",math.gcd(21,45))
print("Radian to degree function",math.degrees(30))
print("Degree to radian function",math.radians(30))

print("Complex no: ", "\n", "(3+5j) + (5 + 2j) = ",(3+5j) + (5 + 2j))


Output


Python supports other types of numbers, such as Decimal and Fraction. Python also has built-in support for complex numbers, and uses the j or J suffix to indicate the imaginary part (e.g. 3+5j).

There are many other functions such as log, trigonometric functions 

Read More

Introduction Of Python

August 29, 2017

Hello friends, lets learn python programming!!!
Have you ever wondered why Python is named as Python???

Lets see the features and other advantages of Python.



Python is easy to learn and is a high level language. Python offers much more structure and support for large programs than shell scrips or batch files.

Python allows you to split your program into modules that can be reused in other Python programs. It comes with a large collection of standard modules that you can use as the basis of your programs — or as examples to start learning to program in Python. Some of these modules provide things like file I/O, system calls, sockets, and even interfaces to graphical user interface toolkits like Tk.

Python is an interpreted language, which can save you considerable time during program development because no compilation and linking is necessary. The interpreter can be used interactively, which makes it easy to experiment with features of the language, to write throw-away programs, or to test functions during bottom-up program development. It is also a handy desk calculator.

Python enables programs to be written compactly and readably. Programs written in Python are typically much shorter than equivalent C, C++, or Java programs, for several reasons:
  • the high-level data types allow you to express complex operations in a single statement;
  • statement grouping is done by indentation instead of beginning and ending brackets;
  • no variable or argument declarations are necessary.
Python is extensible: if you know how to program in C it is easy to add a new built-in function or module to the interpreter, either to perform critical operations at maximum speed, or to link Python programs to libraries that may only be available in binary form (such as a vendor-specific graphics library). Once you are really hooked, you can link the Python interpreter into an application written in C and use it as an extension or command language for that application.

By the way, the language is named after the BBC show “Monty Python’s Flying Circus” and has nothing to do with reptiles. Making references to Monty Python skits in documentation is not only allowed, it is encouraged!


References: https://docs.python.org/3/tutorial/appetite.html
Read More

Print Function

August 29, 2017

Hello friends! This is a simple program to print on Screen


To print the content we simply use the print() function.
Whatever is written inside the double or single quotes gets printed.

Check out the following example:



print ("Welcome to TechandCoders site")

# we can also use single quotes

print ('Let us learn python programming')

# '\' escape character

print ("She's here!")
print('She\'s here!!!')






Now, suppose you want to print the value of a variable. How would you do it?

x = 5

# Simply write the name of the variable without any single or double quotes.

print (x)

Run this code and check


Please comment and share!
Any queries, mail me at: techandcoders@gmail.com

Thank you!

Read More

Saturday, 19 August 2017

Conditional Statement in Java

August 19, 2017

Program to calculate the percentage of given 3 subjects and print the appropriate grade.

The if construct:

if(condition)
{
    // The desired operation
}




The if-else construct:

if(condition)
{
    // The desired operation
}else{
  // The desired operation
}



if - else if - else

if(condition1)
{
    // The desired operation
}else if(condition2)
{
    // The desired operation
}else
{
    // The desired operation
}


public class Grade
{
 public static void main(String args[])
 {
  int phy,chem,math;
  double a;
  phy=69;
  chem=81;
  math=80;
  a=(phy+chem+math)/3;
  System.out.println("Percentage: "+a);
  if(a>=75)
  {
   System.out.println("Grade:A");
  }
  else if(a>=50)
  {
   System.out.println("Grade:B");
  }
  else if(a>=35)
  {
   System.out.println("Grade:C");
  }
  else
  {
   System.out.println("Fail");
  }
 }
}
OUTPUT
Percentage: 76.0
Grade:A

Read More

Factorial of a number in Java

August 19, 2017
Hello everyone,

The factorial is denoted by "!"

So when we say, find 3 factorial, it is mathematically written as 3!

Now let us look into the formula as to how to find the factorial of any number.
Factorial of a number n is given by n! = n(n-1)(n-2)!

Suppose, factorial of 5 is 5 x 4 x 3 x 2 x 1 = 120.

The two important facts you should know about factorial: 
  • The factorial of 1! = 1
  • The factorial of 0! = 1

Now let us write to program to find the find the factorial of a given number.

import java.util.Scanner;
public class Factorial
{
 public static void main(String args[])
 {
  int i, fact=1;
  System.out.println("Enter the Number:");
  Scanner in= new Scanner(System.in);
  int a=in.nextInt();
  for(i=1;i<=a;i++)
  {
   fact=fact*i;
  }
  System.out.println(factorial +"!="+fact);
 }
}
OUTPUT
Enter the Number:
5
Factorial != 120


Read More

Hello world in Java

August 19, 2017

Hello Friends,
This is a program to print a simple message:

Remember: The file name should be same as the class Name.

In this case, the file name will be First.java

class First
{
    public static void main(String args[ ])
    {
        System.out.println("hello World");
    }
}
OUTPUT
hello World
Read More

Sunday, 6 August 2017

Stack using link list implementation

August 06, 2017
Hello Friends,

#include<stdio.h>
#include<stdlib.h>
struct stack
{
 int data;
 struct stack *next;
};
struct stack *top = NULL;
struct stack *getNode()
{
 return (struct stack*)malloc(sizeof(struct stack));
}
void push()
{
 int val;
 struct stack *newnode;
 printf("\n Enter data :");
 scanf("%d",&val);
 newnode = getNode();
 newnode->data = val;
 if(top == NULL)
 {
  top = newnode;
  newnode->next = NULL;
 }
 else
 {
  newnode->next = top;
  top = newnode;
 }
}
void pop()
{
 int x;
 struct stack *del,*temp;
 temp = top;
 if(temp== NULL)
  printf("\n stack underflow");
 else
 {
  x=temp->data;
  top = temp->next;
  free(temp);
  printf("\n Deleted element is %d",x);
 }
}
void peek()
{
 if(top == NULL)
  printf("\n Stack Underflow");
 else
 {
  printf("Peek value : %d \n ",top->data);
 }
}
void disp()
{
 struct stack *temp=top;
 if(top == NULL)
  printf("\n Stack underflow \n");
 else
 {
  printf("\n Stack elements are : ");
  while(temp!=NULL)
  {
   printf("%d \t",temp->data);
   temp = temp->next;
  }
 }
}
void quit()
{
 printf("\n Thank you!!! \n\n");
}
void main()
{
 int ch;
 do{
  printf("\n == MENU ==");
  printf("\n 1. PUSH ");
  printf("\n 2. POP ");
  printf("\n 3. PEEK ");
  printf("\n 4. DISPLAY ");
  printf("\n 5. QUIT");
  printf("\n Enter your choice :");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1 : push();
      break;
   case 2 : pop();
      break;
   case 3 : peek();
      break;
   case 4 : disp();
      break;
   case 5 : quit();
      break;
   default : printf("\n Invalid input");
  }
 }while(ch!=5);
}
/*
OUTPUT : 



 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :1

 Enter data :50

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :1

 Enter data :20

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :1

 Enter data :30

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :1

 Enter data :40

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :1

 Enter data :10

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :4

 Stack elements are : 10  40  30  20  50  
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :3
Peek value : 10 
 
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :2

 Deleted element is 10
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :4

 Stack elements are : 40  30  20  50  
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :2

 Deleted element is 40
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :2

 Deleted element is 30
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :4

 Stack elements are : 20  50  
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :2

 Deleted element is 20
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :3
Peek value : 50 
 
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :4

 Stack elements are : 50  
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :3
Peek value : 50 
 
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :2

 Deleted element is 50
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :3

 Stack Underflow
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :4

 Stack underflow 

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :5

 Thank you!!! 

*/


Read More
PropellerAds