Category Archives: Python

Python Tutorial

Python Numbers

Numbers The interpreter of Python acts like a simple calculator. You can make operation like add, subtract(minus), multiply, divide. Examples Add >>> 3 + 5 8 >>> 125.5 + 2.5 128.0 >>> 134.7 + 3 137.7 Multiply >>> 3 * 5 15 >>> (2 * 3) * 3 18 >>> (3 + 5) * 4 32 Divide >>>… Read More »

Python Strings

Strings Strings can be enclosed in single quotes (‘..’) or double quotes (“..”). The print() function is used to show the output of strings. Examples Single quotes >>> ‘Single quotes test’ ‘Single quotes test’ Double quotes >>> “Double quotes test” ‘Double quotes test’ Single and Double quotes in a string >>> ‘”Single” and “Double” quotes in a string’… Read More »

Python Lists

Lists The Python list, is a sequence of values, separated by commas between square brackets. Lists can have values of different data types. Example Extract all values from a list with python >>> my_list = [1, 5, 7, 21, 33] >>> my_list [1, 5, 7, 21, 33] Extract value from position 0 and 2 from the list >>>… Read More »

Python IF

IF statement IF statement – with the IF statement you can use optional parts like elif or else. The keyword elif is short for else if keyword. Examples >>> c = 10 >>> if c < 0: print('Negative value') elif c >> >>> a = int(input(“Enter a value: “)) Enter a value: 5 >>> if a < 5:… Read More »

Python FOR

FOR statement The FOR statement iterates over the items of a list or a string, in the order that they appear in the list or string. Examples >>> my_list = [‘Learn’, ‘python’, ‘language’] >>> for n in my_list: print(n) Learn python language >>> my_list=[‘Learn’,’python’,’test’,’language’] >>> for n in my_list: print(n,len(n)) Learn 5 python 6 test 4 language 8… Read More »

Python Range

Range function The Range function determine the number of iterations to be executed by FOR statement. The Range function cycle start with 0 and ends with the n-1 value. Examples Print range values >>> for c in range(3): print(c) 0 1 2 Print min and max values of the range >>> print(range(5)) range(0, 5) Print all values of… Read More »

Python Break

Break statement The Break statement is used in statements like for or while loop. If the condition is met, then you can use break statement to exit the loop. Examples >>> for n in range(1, 5): for x in range (3, n): if x == 4: print(‘Value of n is ‘,n) break else: print(‘No x value’) No x… Read More »

Python Continue

Continue statement The Continue statement is used in statements like for or while loop. If the condition is met, then you can use continue statement to continue the loop. Examples >>> x = 3 >>> for c in range(1,8): if x == 3: print(“My value is: “, c) continue print(“Next value: “, c) My value is: 1 My… Read More »

Python Tuples

Tuples and Sequences The Python Tuple is a another standard sequence data type and consists of a number of values separated by commas. Tuples are immutable (object with a fixed value). Immutable objects (numbers, strings, tuples) can not be modified and can be used like constants. Examples Extract values from a tuple >>> v = 123, 87649, ‘abc’,… Read More »

Python Sets

Sets The Python data type set is an unordered collection with no duplicate elements. The elements inside the set are separated by commas. Examples Extract values from a set >>> my_set = {1, 2, 3, 8, 12} >>> my_set {8, 1, 2, 3, 12} >>> a = {‘test’, 1, ‘aaa’, 4} >>> print(a) {1, 4, ‘test’, ‘aaa’} >>>… Read More »