Wednesday, 28 August 2019

Important Python Reserved Keywords

Reserved Keywords in Python Programming


All Computer programming languages have their own list of reserved keywords. In programming language, reserved keywords are used for internal processing. In Python language, all the keywords are defined using lower-case letters. Followings are the list of Reserved keywords used by Python:

 and  except  lambda
 as  exec  not
 assert  finally  or
 break  for  pass
 class  from  print
 continue  global  raise
 def  if  return
 del  import  try
 elif  in  while
 else  is  with
 yield

Avoid use of reserved keywords in Python programs.

Saturday, 29 June 2019

Top 10 Programming Languages of the World – 2019

TOP PROGRAMMING LANGUAGES OF THE WORLD


If you are a new to the field of programming, the very first question comes to your mind is
  • What to learn first? 
  • Which programming language will be useful to me in 2019?
  • Which will be most suitable language from career point of view?

There are many programming languages available in the field of Computer Science. One of the easiest ways to pick the best programming language to learn is by observing the demand of Software Industry. Some of the survey conducted by popular organizations are given below for your ready reference.

IEEE Spectrum [01]
GeeksForGeeks [02]
Guru99 [03]
FullStackAcademy [04]
1.      Python
2.      C++
3.      Java
4.      C
5.      C#
6.      PHP
7.      R
8.      JavaScript
9.      Go
10.  Assembly
1.      JavaScript
2.      Python
3.      Java
4.      C/CPP
5.      PHP
6.      Swift
7.      C#
8.      Ruby
9.      Objective – C
10.  SQL
1.      Python
2.      Java
3.      R
4.      JavaScript
5.      Swift
6.      C++
7.      C#
8.      PHP
9.      SQL
10.  Go
1.      Java Script
2.      Swift
3.      Java
4.      C/C++
5.      Python
6.      PHP
7.      Ruby
8.      C#
9.      RUST


References:
[01] spectrum.ieee.org/at-work/innovation/the-2018-top-programming-languages [This app/ranking mechanism was originally developed in collaboration with IEEE Spectrum by data journalist Nick Diakopoulous. ]
[02] www.geeksforgeeks.org/top-10-programming-languages-of-the-world-2019-to-begin-with
[03] www.guru99.com/best-programming-language.html
[04] www.fullstackacademy.com/blog/nine-best-programming-languages-to-learn-2018

More details about programming languages and its use will be added soon.

Difference between C and Python

Difference between C and Python


C and Python are general purpose computer programming language. Python is generally preferred for server side scripting. Some of the key difference between C and Python are given below:

Table: Difference between C and Python

C Language
Python Language

Designed by Dennis Richie

Product from Python Software Foundation
C programs are faster but coding is complex and long

Coding is easier and short in Python
C requires type declaration

Python does not require type declaration
C supports in line assignment

Python does not support in line assignment
Data type of a variable must be specified at the time of declaration of variable.

Specifying data type of a variable is not needed while declaring variable.


Basics of Python Programming

Basics of Python Programming

What is Python?

Python Installation

Writing First Python Program

Executing First Python Program

Python Data Types


Tuesday, 2 April 2019

Top 10 Python Practicals for Beginners

Top 10 Python Practicals for Beginners


1. Write a Python program to print your name.

Start your Python Shell.

You will get command prompt like (>>>). Write following simple command to print your first message using Python Programming.

>>> print("K Patel")

Output:

K Patel

Congratulations!!! Your first python program is done.


2. Write a Python program to print your name and address.

>>> print("K Patel.\nAhmedabad, Gujarat. Pin. 380009\nIndia.")

Output:

K Patel
Ahmedabad, Gujarat. Pin. 380009
India.


3. Write a Python program to read and print your name and address using the input() function.

Source code:

name = input("Enter name: "
print(name)

address = input("Enter address:")
print(address)

mobile = input("Enter mobile number:")
print(mobile)

Output:

Enter name: K Patel 
K Patel

Enter address: Ahmedabad, India 
Ahmedabad, India

Enter mobile number:9898989*** 
9898989***


4. Write a Python program to print 1 to 10.

Solution 1:

for i in range(111):
   print (i)

Output:
1
2
3
4
5
6
7
8
9
10


Solution 2:

for i in range(111):
   print (i, end=" ")

Output:

1 2 3 4 5 6 7 8 9 10

5. Python program to display Good Morning message based in given time.

Source code:


time = int(input("Enter time:"))

if time < 12:
    print ("Good Morning...")

print("Have a nice time...")

Output:

Enter time:5
Good Morning...
Have a nice time...


6. Python program to check whether given number is positive or not.

Source code:


number = int(input("Enter a number:"))

if number > 0:
    print ("It's positive number")
else
    print ("It's not positive number")

Output:

Enter a number: 3
It's Positive number


7. Python program to demonstrate nested-if statement. This program will check whether entered number is positive or negative or zero.

Source code:

number = int(input("Enter a number: "))
if number >= 0:
    if number == 0:
        print("It's Zero")
    else:
        print("It's Positive number")
else:
    print("It's Negative number")

Output:

Enter a number: 5
It's Positive number


8. Python program to print sum of 1 to 100 (1+2+3+...+100) using while loop.

Source code:


sum = 0
num = 1

while (num <= 100):
    sum = sum + num
    num = num + 1
print("The sum is", sum)

Output:

The sum is 5050


9. Python program to print square pattern of star.

Source code:


lines = int(input("Enter number of lines:"))

for i in range(lines):    
   for i in range(lines):        
      print('*', end = '  ')    
   print()

Output:

Enter number of lines:3

*  *  *  
*  *  *
*  *  *




10. Python program to print Z to A alphabets.

Source code:


for i in range(90,64,-1):
    print(chr(i), end=' ')

Output:

Z Y X W V U T S R Q P O N M L K J I H G F E D C B A


* * * * *


Friday, 31 August 2018

Python Functions

Python Functions


Function Definition: Function is a group of statements which performs a specific task. It is also known as a Sub-routine or a Procedure or a Method.

In most programming languages two types of functions available:
  • Library Functions (also known as System defined function, for example print(), input(), etc.)
  • User defined functions (written by programmers, for example calculate(), find_area(), etc.)

Let's learn how Python Functions works.

Defining a Python Function

Following simple rules are followed while defining Python Functions:
  • Function blocks begin with the def keyword; followed by the function name and parentheses (  ).
  • Any number of parameters or arguments can be placed within function parentheses.
  • In the beginning of functions, writing a short comments about a function is one of the standard practice. 
  • Function code begins with a colon(:) and has indentation.
  • return statement at the end indicates end of function.

Syntax of Python Function


def function_name( arguments ):
   "function comments"
   function logic / code
   return

By default, arguments or parameters have a positional behavior and you need to write in the same order as they defined.

Python Function Example

The following python function prints line with a specified character as an parameter.

def print_line( chr ):
   "This function prints a line of given character"
   for i in range(0,15)
      print (chr)
   return

print_line('*')
print("")
print(" Welcome to Python Practicals ")
print_line('=')

Output of program

* * * * * * * * * * * * * * * 
 Welcome to Python Practicals 
= = = = = = = = = = = = = = = 

Examples of Popular Python Functions


1. Calculate Factorial

def factorial(n):
    """Calculate the factorial of a number."""
    if n == 0 or n == 1:
        return 1
    return n * factorial(n - 1)

# Example usage
print(factorial(5))  # Output: 120

2. Check if a Number is Prime

def is_prime(num):
    """Check if a number is prime."""
    if num <= 1:
        return False
    for i in range(2, int(num**0.5) + 1):
        if num % i == 0:
            return False
    return True

# Example usage
print(is_prime(7))  # Output: True

3. Find the Largest Element in a List

def find_largest(numbers):
    """Return the largest number in a list."""
    if not numbers:
        return None
    largest = numbers[0]
    for num in numbers:
        if num > largest:
            largest = num
    return largest

# Example usage
print(find_largest([3, 7, 2, 8, 5])) 
# Output: 8

4. Reverse a String

def reverse_string(s):
    """Reverse a given string."""
    return s[::-1]

# Example usage
print(reverse_string("hello"))  
# Output: 
"olleh"

5. Generate all permutations of a string using recursion

def string_permutations(s, prefix=""):
    """Generate all permutations of a string recursively."""
    if len(s) == 0:
        print(prefix)
    else:
        for i in range(len(s)):
            rem = s[:i] + s[i+1:]
            string_permutations(rem, prefix + s[i])

# Example usage
string_permutations("abc")   
# Output:
 abc
 acb
 bac
 bca
 cab
 cba

6. Fibonacci Sequence Generator

def fibonacci(n):
    """Generate a Fibonacci sequence up to n terms."""
    fib_sequence = [0, 1]
    for i in range(2, n):
        fib_sequence.append(fib_sequence[i - 1] + fib_sequence[i - 2])
    return fib_sequence[:n]

# Example usage
print(fibonacci(10)) 
 
# Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Python Classes

Python Classes

Python File Handling

Python File Handling

Python Arrays

Python Arrays

Wednesday, 22 August 2018

What is Python?

Python is one of the widely used high-level programming language invented by Guido van Rossum. It can be used for solving varieties of problems. Python is known for its simplicity and code readability. Programmer can develop application very rapidly using Python.

Python code resembles the English language. To interpret the Python codes, computer needs a special program known as the Python interpreter. Python interpreter is needed to code, test and execute the  Python programs.

Read more about Python Interpreter Installation here.