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 
= = = = = = = = = = = = = = = 

* * * * *

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.


Friday, 29 June 2018

Python nested if statement

Python nested-if statement

Nested if statements in any programming language means an if statement inside another if statement. Python also allows us to nest if statements within if statements. i.e, we can write an if statement inside another if statement.

Syntax of nested-if statement:

if (condition 1):
   # Code to be executed when condition 1 is true
   if (condition 2):
      # Code to be executed when condition 2 is true
   # end of inner if Block
# end of outer if Block

Example of Nested-if Statement: Python program to check Positive, Negative or Zero number.

# Checking for Positive, Negative, or Zero Number
number = int(input("Enter one number:"))
if number >= 0:
    if number == 0:
        print("The number is zero")
    else:
        print("The number is positive")
else:
    print("The number is negative")

Output of program

Enter a number:5
The number is positive


if-elif-else ladder

The if statements are executed from the top to the bottom sequentially. When one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the last else statement written will be executed.

Syntax of if-elif-else

if (condition 1):
    statements for condition 1
elif (condition 2):
    statements for condition 2
.
.
else:
    statements for else

Example of Python if-elif-else ladder

number = int(input("Enter number in range of 1 to 5:"))
if (number == 1):
 print ("It’s One")
elif (number == 2):
 print ("It’s Two")
elif (number == 3):
 print ("It’s Three")
elif (number == 4):
 print ("It’s Four")
elif (number == 5):
 print ("It’s Five")
else:
 print ("It’s not in range of 1 to 5")

Output of program

Enter number in range of 1 to 5:3
It’s Three

Suggested Articles for you:

if-else statement


if statement

Python provides following decision making statements (also known as conditional statements):

  • if statement
  • if..else statement
  • nested - if statement

if statement


if statement is one of most commonly used conditional statement of Python. "if statement" is written by using the if keyword. if statement consists of a Boolean expression which is followed by one or more logical statements.

Example 1: Use of if statement to check greater number.

number1 = 101
number2 = 201

if number2 > number1:
    print ("number2 is greater than number1.")

if number1 > number2:
    print ("number1 is greater than number2.")


Output of Program

number2 is greater than number1.

Example 2: Python program to display Good Morning message based in given time.

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

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

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

Output of Program

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

Example 3: Python program to check whether given number  is positive or not.

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

if number > 0:
    print ("It's positive number")
print("It's end of program")

Output of Program

Enter your time:5
It's end of program

#Note: If entered number is negative or zero then this program will print only "It's end of program" message.

continue statement


break statement

break statement

Python Operators

Python Operators

Like all computer programming languages, python also supports following basic operators. Using these operators, we can perform mathematical operations on variables.

Basic operators in Python includes

+ for addition operation
-  for subtraction operation
*  for multiplication operation
/  for division operation
%  for modulus operation
**  for exponent operation
//  for floor division operation

Examples of Python Operators

Let’s use these operators on following two variables:

num1 = 10
num2 = 5

Addition
num1 + num2 will result in 15

Subtraction
num1 - num2 will result in 5

Multiplication
num1 * num2 will result in 50

Division
num1/num2 will result in 2

Floor Division
num1//num2 will result in 2 (rounds down the answer to the nearest whole number)

Modulus
num1%num2 will result in 0 (It’s remainder of 10 divided by 5)

Exponent
num1**num2 will result in 100000 (10 to the power of 5)

Assignment Operator (=)


Like most of the programming languages, Python uses equal sign (=) as assignment operator.

number = 5

Above statement will assign value 5 to variable number.

There are a few more ways to use assignment operators in Python; examples are +=, -= and *=.

If we want to increment value of number by 2, we can write

number = number + 2

Python will first evaluate the expression on the right side i.e. number + 2 and assign the answer to the left side. Hence execution of above statement will result in number = 7 (assuming initial value of number is 5).

The same statement can also be written as under:

number += 2

The number += 2 is actually a short form of the statement number = number + 2

Similarly, if we want to do a subtraction, we can write number = number – 2 or number -= 2. The same works for most of the python operators described above.



* * * * *

Python Installation and Setup

Python Installation and Setup

Python interpreter is needed to run Python programs. Perform following steps to install python software in your system.


Step-1 Search "Python Download in Google"


Step-2 Download latest Python version from URL https://www.python.org/downloads/


Step-3 Install Downloaded - Python 3.7.4 applications (.exe file)


Step-4 Install Python 3.7.4 - Customize your installation


Step-5 Select basic configuration to install (select your installation drive and folder)



Step-6 Wait for message "Python Setup was successful"


Step-7 Go to Windows start menu and select: IDLE Python 3.7


Step-8 Start writing your Python code in IDLE (Python Interpreter)

First Python Practical using Python Shell

* * * * *



Acknowledgement: All product names (Google, Windows, Python and others), logos, and brands are property of their respective owners. The content shown on page is for demonstration only.

Decision Making

Decision Making

Basic Syntax

Welcome to Python Practicals

Let's explore Top Python Practicals

Click your Topic of Interest to Learn

Introduction to Python

Python is an easy to learn and very powerful programming language. It has efficient data structures and a simple approach to object-oriented programming. Python’s syntax together with its interpreted nature, make it an ideal language for scripting and rapid application development. 

Basics of Python

Python is an easy to learn and very powerful programming language.

Data Types

Python is very good in String Processing. 

Numbers

Numbers are one of the fundamental building block of any programming language.


Copyright © 2021 PythonPracticals.Blogspot.Com

All rights reserved.






Python Overview

Python Overview

If you are new to the Computer Programming, Python is a great place to start. One of the key features of Python is its simplicity which makes it ideal language for beginners to learn.

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.

Most programs in Python needs very few lines of code to solve complicated programming task compared to other languages. Fewer lives of code leads to fewer programming errors and reduces the overall development time. 



Python Data types

Data types

Friday, 23 March 2018

First Python Program

My First Python Program

Start your Python Shell.

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

>>> print("Hello World")
Hello World

Congratulations!!! Your first python program is done....

Here, >>> print("Hello World") is the python code we need to write to generate the output Hello World.

* * * * *
#Sample Python Program
print("Hello Students..")
print("Welcome to Python Practicals.......")

Output:
Hello Students..
Welcome to Python Practicals.......


Python Practicals

Welcome to the World of Python Programming


This blog helps you to learn Python programming concepts. You can learn Python programming practicals at your own pace. One can easily learn concepts of Python programming by practicing various programs given on various pages of this blog. Enjoy the power of Self Study.

Click your Topic of Interest to Learn.....


Basics of Python
Looping
Functions
File Management
Classes
Networking
Security



Branching Statements in Python: Python language executes program statements in a sequence. Sometimes we need to alter the flow of sequence of statements. This is possible using Branching statements offered by Python language. They are also known as control statements. Branching Statements of Python are given below:
  • if statement
  • if…else statement
  • nested if statement


Looping statements in Python: In computer programming, a loop is a sequence of commands or statements that are repeated many times. Looping statements in Python language are as under:
  • for statement
  • while statement
  • nested loops

File Management in Python: We create a File for permanent storage of data. Python language provides many functions that help us to perform various file operations. 

Click here to know more about general syntax of File Management, reading content from a file, writing and appending content to a file. 


* * * * *