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,…

Hashing Strings with Python

Hashing Strings with Python

A hash function is a function that takes input of a variable length sequence of bytes and converts it to a fixed length sequence. It is a one way function. This means if f is the hashing function, calculating f(x) is pretty fast and simple, but trying to obtain x again will take years. The value returned by a hash function is often called a hash, message digest, hash value, or checksum. Most of the time a hash function will produce unique output for a given input. However depending on the algorithm, there is a possibility to find a collision due to the mathematical theory behind these functions. Now suppose you want to hash the string “Hello Word” with the SHA1 Function, the result is 0a4d55a8d778e5022fab701977c5d840bbc486d0. Hash functions are used inside some cryptographic algorithms, in digital signatures, message authentication codes, manipulation detection, fingerprints, checksums (message integrity check), hash tables, password storage and much more. As a Python programmer you may need these functions to check for duplicate data or files, to check data integrity when you transmit information over a network, to securely store passwords in databases, or maybe some work related to cryptography. Python Programming – Built-in Functions Python Programming – Defining A Function Python Programming –…

Python Modules Urllib2 User Agent

Python Modules Urllib2 User Agent

Overview This post will show how to add headers to a HTTP request. By default urllib2 identifies itself as Python-urllib/2.7 : GET / HTTP/1.1″ 200 151 “-” “Python-urllib/2.7” That can sometimes be confusing for certain sites. With the user_agent header in Python, it’s possible to alter that and specify any identity you like. The example below, use the Mozilla 5.10 as a User Agent, and that is also what will show up in the web server log file. Also Read: Python Program to Calculate Age in Days from Date of Birth Google Command Line Script Scraping websites with Python CommandLineFu with Python VEDL Pivot Point Calculator import urllib2 req = urllib2.Request(‘http://192.168.1.2/’) req.add_header(‘User-agent’, ‘Mozilla 5.10’) res = urllib2.urlopen(req) html = res.read() This is what will show up in the log file. “GET / HTTP/1.1” 200 151 “-” “Mozilla 5.10”

Command Line speedtest.net via tespeed

Command Line speedtest.net via tespeed

Overview While looking around for a command line tool to check my network speed, I stumbledupon a post on lowendtalk.com. What is it? It’s a command line interface to the speedtest.net servers and can be found here: https://github.com/Janhouse/tespeed What is Speedtest? Speedtest.net is a connection analysis website where users can test their internet speed against hundreds of geographically dispersed servers around the world. At the end of each test,users are presented with their download (the speed of data from the server to their computer) and upload (the speed of sending data from the user’s computer to the server) bandwidth speeds. Download and Install Python Activate Admin Application for Your Python Django Website How to Install Django on Windows, Mac and Linux PVRINOX Pivot Point Calculator Requirements This script requires lxml and argparse, so make sure you install that first. See this link for a howto. In most cases it’s enough to run this command from a terminal: pypm install lxml How does the script work? See the this url for how to use the program Running the script After downloading the requirements I decided to give this program a chance. Copy and paste the code from here. Run the script Open up your editor, copy and paste…

How to Best Use Try-Except in Python

How to Best Use Try-Except in Python

Exception handling allows us to enforce constraints on variables to implement our business logic in the computer program and it also enables us to write a robust program that can handle different types of errors occurred during execution. In python, we use try-except blocks to implement exception handling. In this article, we will look at some ways to write an efficient python program by looking at illustrations to best use try-except in python. So, let’s dive into it. Read Also: Java Program to Compute 2(a2+b2) where Value of a and b are Given Raising Exceptions Manually using raise keyword A python program throws inbuilt exceptions automatically when an error occurs but we can also raise inbuilt exceptions manually using raise keyword. By throwing inbuilt exceptions using the raise keyword, we can use the inbuilt exceptions to enforce constraints on the variables to make sure that program is correct logically. Here the point to keep in mind is that a program may not throw any error but it may give an unrealistic value as output when proper constraints are not applied to the variables. Hence we may use raise keyword to throw exceptions in the python try-except block to enforce the…

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…

Introduction to Python Classes (Part 1 of 2)

Introduction to Python Classes (Part 1 of 2)

Classes are a way of grouping related bits of information together into a single unit (also known as an object), along with functions that can be called to manipulate that object (also known as methods). For example, if you want to track information about a person, you might want to record their name, address, and phone number, and be able to manipulate all of these as a single unit. Python has a slightly idiosyncratic way of handling classes, so even if you’re familiar with object-oriented languages like C++ or Java, it’s still worth digging into Python classes since there are a few things that are different. Before we start, it’s important to understand the difference between a class and an object. A class is simply a description of what things should look like, what variables will be grouped together, and what functions can be called to manipulate those variables. Using this description, Python can then create many instances of the class (or objects), which can then be manipulated independently. Think of a class as a cookie-cutter – it’s not a cookie itself, but it’s a description of what a cookie looks like, and can be used to create many individual cookies, each of which can be…

How to Calculate Exponents using Python

Using Exponents in Python | How to Calculate Exponents using Python? | Python math.exp() Method

Are you wondering about using exponents? Do you need help in programming exponents logic using python? Then, here is the perfect answer for all your queries. The tutorial on how to calculate exponents in python will make you understand clearly about using exponents in python. There are few ways to calculate exponents using python. Want to explore what are they? get into this tutorial and understand calculating the exponential value in python. How to Calculate Exponents using Python? Calculate Python exponents with the ** Operator Calculate Python exponents with the pow() function Using Python math.exp() Method Using math module’s pow() function How to Calculate Exponents using Python? In multiple ways, python permit users to calculate the exponential value of a number. Let’s take a look at them in a detailed way: 1. Calculate Python exponents with the ** Operator To raise a number to the power of another number, you need to use the “**” operator. Where multiplying two numbers only uses one * symbol, the operator for raising one number to the power of another uses two: **. Let’s see an illustration. To obtain 4 squared (4 raised to the power of two is another way of saying it),…

Using math in python

Using math in python

Math in Python The Python distribution includes the Python interpreter, a very simple development environment, called IDLE, libraries, tools, and documentation. Python is preinstalled on many (if not all) Linux and Mac systems, but it may be an old version. Read Also: Python Program to Find Number of Rectangles in N*M Grid Calculator To begin using the Python interpreter as a calculator, simple type python in the shell. >>> 2 + 2 4 >>> 4 * 2 8 >>> 10 / 2 5 >>> 10 – 2 8 Loops in Python Python : List examples Using Exponents in Python | How to Calculate Exponents using Python? | Python math.exp() Method Counting with variables Put in some values into variables to count the area of a rectangle >>> length = 2.20 >>> width = 1.10 >>> area = length * width >>> area 2.4200000000000004 Counter Counters are useful in programming to increase or decrease a value everytime it runs. >>> i = 0 >>> i = i + 1 >>> i 1 >>> i = 1 + 2 >>> i 3 Counting with a While Loop Here is an example showing when it’s useful to use counters >>> i = 0…

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…