Python Programming - If-Else Statement

Python Programming – If-Else Statement

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better. Python Programming – If-Else Statement The statement if also allows answering for the false value by using an else clause. The else statement con¬tains the block of code that executes if the conditional expression in the if statement evaluates to 0 or a false value. The else statement is an optional statement; if it is provided, in any situation one of the two blocks gets executed, not both. The syntactic form of the if-else statement is as follows: if (expression): statements BLOCK 1 else: statements BLOCK 2 When the expression evaluates to a non-zero value, the condition is considered to be true, and statements BLOCK 1 are executed; otherwise, statements BLOCK 2 are executed. Blocks can have multiple lines. As long as they are all indented with the same amount of space, they constitute one block. Figure 5.5 shows the flow diagram of the if-else statement. For example, if 10>100: print ( ” 10 is greater than 100 ” ) else: print ( ” 10 is less than 100 ” ) Python Programming – Conditional and Iterative Statements Python Programming – Flowcharts for Sequential,…

Python Programming - Date and Time Functions

Python Programming – Date and Time Functions

You can learn about Functions in Python Programs with Outputs helped you to understand the language better. Python Programming – Date and Time Functions Classes provide a number of functions to deal with dates, times, and time intervals. Date and DateTime are an object in Python, so when you manipulate them, you are actually manipulating objects and not strings or timestamps. Whenever you manipulate dates or times, you need to import the DateTime function. The DateTime module enables you to create the cus¬tom date objects, and perform various operations on dates like the comparison, etc. To work with dates as date objects, you have to import the DateTime module into the Python source code. The concept of Ticks In Python, the time instants are counted since 12:00 am, January 1, 1970. It is the beginning of an era. and is used to start counting time. This special moment is called an epoch. In Python, the time between the present moment and the special time above is expressed in seconds. That time period is called Ticks. A tick can be seen as the smallest unit to measure the time. The time ( ) function in the time module returns the number of seconds…

Python Programming – Print Statement

Python Programming – Print Statement

You can learn about Introduction to Python Programming Programs with Outputs helped you to understand the language better. Python Programming – Print Statement print is a function that prints something on the screen. To print any message as it is on screen, just type print and that message or argument in (”) or (” “). print (“Hello World”) For example, in the above-mentioned program, “Hello World” is a string and print is a Python com¬mand to write a string in quotes on the console or terminal window. print( ) statement without any argument will simply jump to the next line. Now, let us consider some more examples of how to use the print command. Type the following print statements and run the program to see the output: Python Programming – How To Start Python Python Programming – Creating a Simple “Hello World” Program Python Programming – Statements Example 4. Program to display string using the print command. print ( ” Hello, How are you? ” ) print ( ” I am learning Python language . ” )RUN >>> Hello, How are you? I am learning Python language. >>> Let’s do something more advanced: Example 5. Program to display sum of two numbers…

Python Programming – Accepting Input From The Console

Python Programming – Accepting Input From The Console

You can learn about Introduction to Python Programming Programs with Outputs helped you to understand the language better. Python Programming – Accepting Input From The Console In Python, input is used to request and get information from the user, and print is used to display information on the screen. input ( ) In Python, input is accomplished by using an assignment statement combined with a special function called input( ). Syntax of input ( ) is as follows: variable = input(<prompt>) Here, the prompt is an expression that serves to ask the user for input; this is almost always a string literal (i.e., some text inside quotation marks). When Python encounters an input expression, it evaluates the prompt and displays the result of the prompt on the screen. It then pauses and waits for the user to type an expression and press the Enter key. The expression typed by the user is then evaluated to produce the result. As the input provided is evaluated, Python expects a valid expression. If the input provided is not correct then either syntax error or exception is raised by Python. Python Programming – How To Start Python Python Programming – Variable Python Programming – Flowcharts for…

Python Programming – Simple Python Programs

Python Programming – Simple Python Programs

You can learn about Introduction to Python Programming Programs with Outputs helped you to understand the language better. Python Programming – Simple Python Programs Program to ac,d two numbers given by the user. # Program to add two numbers given by user print ( ‘ Please enter an integer value: ‘ ) x = input ( ) print ( ‘ Please enter another integer value: ‘ ) y = input ( ) numl = int(x) mm2 = int (y) print (numl, ‘ + ‘, mm2, ‘=’, num1 + num2) Introduction to Python 77 Module 3: M3-R5 RUN >>> Please enter an integer value: 56 Please enter another integer value: 32 56 + 32 = 88 >>> Python Programming – Print Statement Python Programming – Variable Python Programming – How To Start Python Program to display moths and days for enterd no.of days. # Program to display moths and days for enterd no.of days. a days = int(input(“Enter days: “)) months = days / 30 days = days5% 30 print ( ”Months = ”, int ( Month) , ”Days = ”, days)RUN >>> Enter days: 365 Months = 12 days >>> Program to find selling price. #Program to find selling price costprice=int (…

Python Programming – Expressions

Python Programming – Expressions

You can learn about Operators in Python Programs with Outputs helped you to understand the language better. Also Read: Java Program to Convert Kilogram to Pound and Pound to Kilogram Python Programming – Expressions The expression consists of a combination of values, which can be constant values, such as a string (” Shiva ” ) or a number (12), and operators, which are symbols that act on the values in some way. The following are the examples of expression: 10 – 4 11 * ( 4 + 5 ) X – 5 a / b In other words, the expression consists of one or more operands and one or more operators linked together to compter a value. for example: a = 5 # assume a = 5 a + 2 is a larger expression that results in the sum of the value in the variable a is also an expression as is the constant 2 since they both represent the value. expression value, in turn, can act as Operands for Operators. For example, 9+6and 8+5+2 are two expressions that will result in a value of 15. taking another example, 6.0/5+(8-5.0) is an expression in which values of different data types are used.…

Introduction to Python Programming – Algorithms

Introduction to Python Programming – Algorithms

You can learn about Introduction to Programming Using Python Programs with Outputs helped you to understand the language better. Also Read: Python Programming Examples with Output Introduction to Python Programming – Algorithms Algorithms A set of instructions that describes the steps to be followed to carry out an activity is called an algorithm or procedure for solving a problem. If the algorithm is written in the computer programming language, then such a set of instructions is called a program. The task of writing the computer program involves going through several stages. So, programming requires more than just writing programs. That means it requires writing algorithms, as well as testing the program for all types of errors. The algorithm is a stepwise presentation of the program in simple English. Characteristics of Algorithm (a) Language independent. (b) Simple, complete, unambiguous, step-by-step program flow. (c) No standard format or syntax required. (d) Helpful to understand problems and plan out solutions. Python Programming – Introduction to Python Interpreter and Program Execution Introduction to Python Programming – Programming Languages Introduction to Python Programming – Testing and Debugging Developing or Writing the Algorithms The process of developing an algorithm (to solve a specific problem) is a trial-and-error process…

Python Programming - Numbers

Python Programming – Numbers

Python Programming – Numbers A number is an object in Python, and it is created when some value is assigned to it. The number data type is used to hold numerical data. Python language supports four different numeric data types as described below: • int (signed integers): Alike C/C++/Java, Python supports integer numbers, which are whole numbers positive as well as negative having no decimal point. • long (long integers): Similar to integers but with limitless size. In Python long integers are followed by a lowercase or uppercase L. • float (floating point real values): Alike C/C++/Java, Python supports real numbers, called floats. These numbers are written with-a decimal point or sometimes in a scientific notation, with exponent e such as 5.9e7 i.e. 5.9xl07, where e represents the power of 10. • complex (complex numbers) : Unlike C/C++/Java, Python supports complex data type. It holds complex numbers of the form x+iy, where x and y are floating-point numbers and i is iota representing the square root of -1 (imaginary number). Complex numbers have two parts where x is known as the real part and y is called the imaginary, part as it is with iota. Unlike C/C++/Java, in Python, the…

Python Programming - Arithmetic Operations On Array

Python Programming – Arithmetic Operations On Array

You can learn about NumPy in Python Programs with Outputs helped you to understand the language better. Read Also: Python Program to Find Leaders in an Array/List Python Programming – Arithmetic Operations On Array NumPy is not only about efficient storing of data but also makes it extremely easy to perform the arithmetic operations on multi-dimensional arrays directly. In the following example, the arithmetic operations are performed on the two multi-dimensional arrays. Example Demo of arithmetic operations. import numpy as np a = np.array([[10,20,30], [10,15,4]]) b = np.array![[2,4,8] , [5, 19, 29]]) print(“Sum of array a and b\n”,a+b) print(“Product of array a and b\n”,a*b) print(“Division of array a and b\n”,a/b)RUN >>> Sum of array a and b [[12 24 38] [15 34 33]] Product of array a and b [[ 20 80 240] [ 50 285 116]] Division of array a and b [ [5. 5 . 3.75 ] [2. 0.78947368 0.13793103]] >>> Python Programming – Introduction To Numpy Python Programming – Array Creation Python Programming – Array Attributes Let’s Try import numpy as np x = np. array ( [ [12,16,15] , [10,15,4] ] ) y = np.array( [ [2,4,5], [5, 3, 2]]) print(“Sum of array\n”,x+y) print(“Product of array \n”,x*y) print(“Division…

Python Programming - Pattern Matching

Python Programming – Pattern Matching

You can learn about Functions in Python Programs with Outputs helped you to understand the language better. Also Read: Java Program to Convert Foot to Yard and Yard to Foot Python Programming – Pattern Matching Regular expressions, called regexes for short, are descriptions for a pattern of text. A regular expression in a programming language is a special text string used for describing a search pattern. It is extremely useful for extracting information from text such as code, files, logs, spreadsheets, or even documents. They are used at the server side to validate the format of email addresses or passwords during registration, used for parsing text data files to find, replace or delete certain strings, etc. It is also used for webpage “Scraping” (extract a large amount of data from websites). In Python, a regular expression is denoted as RE (REs, regexes, or regex pattern) are imported through re module, “re” module included with Python primarily used for string searching and manipulation. In order to use the search ( ) function, you need to import re first and then execute the code. >>> import re Python Programming – Types Of Functions Python Programming – How To Start Python Python Programming – Expressions…