Skip to main content

Posts

Showing posts from April, 2020

Python Turtle Module

Python turtle module To check whether you have  turtle module, open the Python interpreter and type this: > import turtle > bob = turtle.Turtle() When you run this code, it should create a new window with small arrow that represents the turtle. Close the window. Create a file named mypolygon.py and type in the following code: import turtle bob = turtle.Turtle() print(bob) turtle.mainloop() The turtle module (with a lowercase ’t’) provides a function called Turtle (with an uppercase ’T’) that creates a Turtle object, which we assign to a variable named bob. Printing bob displays something like: <turtle.Turtle object at 0xb7bfbf4c> This means that bob refers to an object with type Turtle as defined in module turtle. mainloop tells the window to wait for the user to do something, although in this case there’s not much for the user to do except close the window. Once you create a Turtle, you can call a method to move it around the window. A method is similar to a function...

Python Function calls

Here is example of  function call: > type(56) <class 'int'> The name of the function is type. The expression in parentheses is called the argument of the function. The result, for this function, is the type of the argument. It is common to say that a function “takes” an argument and “returns” a result. The result is also called the return value. Python provides functions that convert values from one type to another. The int function takes any value and converts it to an integer, if it can, or complains otherwise: > int('56') 56 >int('Hello') ValueError: invalid literal for int(): Hello int can convert floating-point values to integers, but it doesn’t round off; it chops off the fraction part: >>> int(5.9999) 5 >>> int(-5.3) -5 float converts integers and strings to floating-point numbers: > float(56) 56.0 > float('6.24158') 6.24158

Python Math functions

Python has the  math module.  What Is Module? A module is a file that contains a collection of related functions. Before we can use the functions in a module, we have to import it with an import statement: > import math This statement creates a module object named math. If you display the module object, you get some information about it: >math <module 'math' (built-in)> The module object contains the functions and variables defined in the module. To access one of the functions, you have to specify the name of the module and the name of the function, separated by a dot (also known as a period). This format is called dot notation. >>> ratio = signal_power / noise_power >>> decibels = 10 * math.log10(ratio) >>> radians = 0.7 >>> height = math.sin(radians) The first example uses math.log10 to compute a signal-to-noise ratio in decibels (assuming that signal_power and noise_power are defined). The math module also provides log, whi...

Python String operations

we can’t perform mathematical operations on strings, even if the strings look like numbers, so the below example would not work: 'fruits'-'vegetables' 'bread'/'easy' 'egg'*'b clean' But there are two exceptions, + and *. The + operator performs string concatenation, which means it joins the strings by linking them end-to-end. For eg: > first = 'fruits' > second = 'vegetables' > first + second fruitsvegetables The * operator also works on strings; it performs repetition. For example, 'fruits'*2 is 'fruits'. Note:Here one value is string and another one is number.

Python Arithmetic operators

Python provides operators, which are special symbols that represent computations like addition and multiplication. The operators +, -, and * perform addition, subtraction, and multiplication, as shown below eg: >20 + 15 35 > 23 - 1 22 > 9 * 8 72 The operator / performs division: > 24 / 2 12.0 the operator ** performs exponentiation; that is, it raises a number to a power: > 2**2 + 5 9  bitwise operator called XOR. > 10 ^ 4 14

Python First Program

 first program we write in a new language is called “Hello, World!” because all it does is display the words “Hello, World!”. In Python, it looks like this: > print('Hello, World!') This is an example of a print statement,  it doesn’t actually print anything on paper. It displays a result on  screen. In this case, the result is words Hello, World! In Python 2, the print statement is  different; it is not a function, so it doesn’t use parentheses. >print 'Hello, World!'