Skip to main content

Posts

Showing posts from May, 2020

Code Simple Calculator in python

  We are going to write code for a simple calculator in python, It will show you that how can you use basic python statements and function to make a calculator that will do addition, subtraction,multiplication and division. Calculator python coding: def add (a , b): return a + b def subtract (a , b): return a - b def multiply (a , b): return a * b def divide (a , b): return a / b print ( "Select Desired Operation." ) print ( "1.Add" ) print ( "2.Subtract" ) print ( "3.Multiply" ) print ( "4.Divide" ) while True : choice = input ( "Enter choice(1/2/3/4): " ) if choice in ( '1' , '2' , '3' , '4' ): num1 = float ( input ( "Enter first number: " )) num2 = float ( input ( "Enter second number: " )) if choice == '1' : print (num1 , "+" , num2 , "=" , add(num1 , num2)) elif choice == ...

Using input command in python

Today i will show you that how can you use input command in python language, so we will write a simple program that will ask for any name or value and then print that name or value. So here is our simple program. a= input ( "Enter your name" ) print (a) as you can see we have declared "a" a variable that will ask for input "Enter your name" and then print command will print the variable "a". So anything we will type will be printed. As show in the picture below. But here you have to remember, if you are working with integers for some mathematical work than you have to declare input type as integer because then python will take input type as integer and not as word. As show in the picture below. As you can see the syntax that how can we declaring the input type,,int, we have to write int then in brackets we have to enter the input command, int stands for integer and it will declare our input as integer, we have and in print command we have instruc...

Writing first python program

  We are here to write our first program,it will  be an easy program just printing the entered word or value. The syntax of python language is very simple and easy and you dont have to confuse your mind with any complexity. So here is our first program print ( "Hello my name is abc" ) print command is used to display the output written in brackets with inverted commas. As we have typed the sentence "Hello my name is abc". So this command will simply print this sentence as show above in the picture.  

Functions

FUNCTIONS Functions are the already defined part of the program which can be recalled from other parts of the program as many times as needed. Functions plays an important role in helping to reduce the program code. Function has basic three parts: 1. Function Definition 2. Function Call 3. THE FUNCTION DEFINITION The FUNCTION DEFINATION: The function definition deals with purpose/task of the function. It tells the python what the function does. Syntax: Def function_name(arguments): Function body. Example: Def Add(a,b): Print(a+b) Arguments are the variables used in function. You cannot skip variable name in function definition. THE FUNCTION CALL Function call passes the variable values to function definition, the number of values must match the number of parameters. Syntax: Function_name(values) Example: Add (5,6) Gives output 11 RETURN STATEMENT : Return statement can be used to return function result to function call: Def add(a,b): Return (a+b) Now return sends the value of sum back ...

Loop Statements

Loop STATEMENTS The Loop statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled. The iteration statements are also called loops or looping statements. THE FOR LOOP The for loop is the easiest to understand of the python loops. Syntax: For I in range(lowerlimit,upperlimit): Loop Body The program will run until I value becomes equal to upperlimit – 1. Example: For I in range(1,5): Print(“Hello”) This program will print Hello 4 times, ie from 1 to 4 (upper limit-1)  I value will start as 1 then loop body will get executed and I value will be incremented. This keeps on taking place until I ==upper limit-1 . After that the program comes out of loop body. THE WHILE LOOP The second loop available in python is the while loop. The while loop is an entry controlled loop. The syntax of a while loop is while (expression): Loop-body Where loop body keep on getting executed until the expression becomes false. When the expression becomes false, th...

Conditional Statements

Conditional STATEMENTS Normally, the statements are executed sequentially as written in the program (i.e. Top to bottom). This is known as the normal flow of control. But sometimes the operational flow of control has to be altered depending upon a condition. The statements that allow to do so, are known as selection statements. Most important selection statement that python offers is the If statement. Selection statements are also known as conditional statements or decision statements. THE IF STATEMENT An if statement tests a particular condition; if the condition evaluates to true, a course of action is followed i.e. a statement of a set of statements is executed. If the condition is false the course of action is ignored. The syntax of if statement is as shown below: if (expression) : Statement THE ELIF STATEMENT In some cases the program requires you to write multiple if statements. This results in many condition evaluations thus slowing up the program. So to avoid th...

Learning Python Basics

                           Python Basics What is VARIABLES in Python? As the name implies the meaning, a variable is something which can be change. A variable is a way of referring to a memory location used by a computer program. A variable is a symbolic name for this physical location. This memory location contains values, like numbers,text or more complicated types. Variables can be treated as a container which can store some value in it. In any other languages for example C++, This is how you write a variable int a = 20 Where int is data type of the variable. Int stands for integer. String refers to text or collection of characters. It is represented in single quotes or double quotes. Float refers to decimal point numbers. In Programming languages like C++ you have to specify the data type of a variable. But in python you don’t have to worry about data type. Whatever value  you give the variable, py...