Posts

Showing posts from October, 2017

Break Statement

Image
Code and Output for val in "12345": if val == "5": break print(val) print("The End")

Whileloop Statement

Image
Code and Output whilestatement count = 0 while (count < 9): print ("The count is:", count) count = count + 1 print ("Good Bye!") infiniteloopstatement var = 1 while var == 1 : #This construct an infinite loop num = input("Enter a number :") print ("You entered: ", num) print ("Good bye!")

Forloop Statement

Image
Code and Output # List of numbers numbers = [ 6, 5, 3, 76, 182 ] # Variable to store the sum sum = 0 # iterate over the list for val in numbers: sum = sum + val # Output: The sum is 272 print ("The sum is", sum)

If Statement

Image
Code and Output ifstatement #from random import randint dice = "6" guess = input("Guess input: ") if guess == dice: print ("Wohoo you got lucky") #if guess == dice: #print ("Wohoo you got lucky") #else: #print ("You guessed wrong") ' ifelsestatement dice = 6 guess = 2 if (guess!=dice): print ("Wohoo you got lucky") else: print ("You guessed wrong") elifstatement dice = 6 guess = 1 if (guess==dice): print("Wohoo you got lucky") elif (guess>dice): print("You guessed Higher") else: print("You guessed lower")

Python List

list1 = [ "abcd", 786, 2.23, "john", 70.2] tinylist = [123, "john"] print (list1)              # Prints complete list print (list1[0])           # Prints first element of the list print (list1[1:3])         # Prints elements start from 2nd til 3rd print (list1[2:])          # Prints elements starting from 3rd elements print (tinylist * 2)       # Prints list two times print (list + tinylist)    # Prints concatenated lists

Continue Statement

Image
1. Explanation of continue statement. The continue statement gives you the option to skip over the part of a loop. The continue statement can be use in both while and for loops. The continue statement cause a program to skip certain factors that come up within a loop, but then continue through the rest of the loop. 2. Syntax for continue statement. continue 3. The flow diagram: 4. Example code: 5. Output: 6. Quiz:

Identity Operators

Image
Code and Output a = 20 b = 20 print ('Line 1','a=',a,':',id(a), 'b=',b,':',id(b)) if ( a is b): print ("Line 2 - a and b have same identity") else: print ("Line 2 - a and b do not have same identity") if ( id(a) == id(b)): print ("Line 3 - a and b have same identity") else: print ("Line 3 - a and b do not have same identity") b = 30 print ('Line 4','a=',a,':',id(a), 'b=',b,':',id(b)) if ( a is not b): print ("Line 5 - a and b do not have same identity") else: print ("Line 5 - a and b have same identity")

Membership Operators

Image
Code and Output a = 10 b = 20 list1 = [1, 2, 3, 4, 5 ] if ( a in list1): print ("Line 1 - a is available in the given list") else: print ("Line 1 - a is not available in the given list") if ( b not in list1): print ("Line 2 - b is available in the given list") else: print ("Line 2 - b is not available in the given list") c=b/a if ( c in list1): print ("Line 3 - c is available in the given list") else: print ("Line 3 - c is not available in the given list")

Logical Operators

Image
Code and Output #Logical for AND x , y = 10, 30 if ( x == 10 and y == 20): print ("Yes") else: print ("No") #Logical for OR a , b = 2 , 5 if ( a == 1 or b == 5): print ("True") else: print ("False") #Logical for NOT z = 10 if not (z == 9): print ("Hello") else: print ("Bye")

Assignment Operator

Image
Code and Output #= is assignment x = 10 print (x) #Output: 10 #+ is assignment with addition a = 20 a += 100 #a = a + 100 print (a) #a = 20 +100 #Output: 120 #- is assignment with subtraction b = 100 b -= 50 #b = b - 50 print (b) #b = 100 - 50 #Output: 50 #*= is assignment with multiplication c = 10 c *= 2 #c = c * 2 print (c) #c = c 10 * 2 #Output: 20 #/= is assignment with division d = 4 d /= 2 #d = d / 2 print (d) #d = d 4 / 2 #Output: 2 #**= is assignment with exponent e = 2 e **= 2 #e = e ** 2 print (e) #e = e 2 ** 2 #Output: #//= is assignment with floor division f = 4 f //= 2 #f = f // 2 print (f) #f = f 4 // 2 #Output: 2 #%= is assignment with modulus division g = 5 g %= 3 #g = g % 3 print (g) #g = g 5 % 3 #Output: 2

Comparison Operator

Image
Code and Output a = 21 b = 10 c = 0 if ( a == b ): print ("Line 1 - a is equal to b") else: print ("Line 1 - a is not equal to b") if ( a != b ): print ("Line 2 - a is not equal to b") else: print ("Line 2 - a is equal to b") #if ( a <> b ): #print ("Line 3 - a is not equal to b") #else: #print ("Line 3 - a is equal to b") if ( a < b ): print ("Line 4 - a is less than b") else: print ("Line 4 - a is not less than b") if ( a > b ): print ("Line 5 - a is greater than b") else: print ("Line 5 - a is not greater than b") a = 5; b = 20; if ( a <= b ): print ("Line 6 - a is either less than or equal to b") else: print ("Line 6 - a is neither less than nor equal to b") if ( b >= a ): print ("Line 7 - b is either greater than or equal to b") else: print ("Line 7 - b is neither greater...

Aritmetic Operator

Image
Code and Output a = 21 b = 10 c = 0 c = a + b print ("Line 1 - Value of c is ", c) c = a - b print ("Line 2 - Value of c is ", c) c = a * b print ("Line 3 - Value of c is ", c) c = a / b print ("Line 4 - Value of c is ", c) c = a % b print ("Line 5 - Value of c is ", c) a = 2 b = 3 c = a**b print ("Line 6 - Value of c is ", c) a = 11 b = 5 c = a//b print ("Line 7 - Value of c is ", c)

Variable advanced

#counter = 100 # An integer assignment #miles = 1000.0 # A floating point #name = "John" # A string #print (counter) #print (miles) #print (name) #counter, miles, name = 100, 100.0, "John" #print (counter, miles, name) #print ("My name is", name) #names = input("Who are you?") #print ("My name is", names) str = "Hello World!" print (str)               # Prints complete string print (str[0])            # Prints first character of the string print (str[2:5])          # Prints  characters starting from 3rd to 5th print (str[2:])           # Prints string starting from 3rd character print (str * 2)           # Prints string two times print (str + "TEST")      # Prints concatenated string print (str , "TEST") print (str[0:5])

Python Basic

#print is for display #print ("Hello World!") #Integer data type #myvar = 1 #print (myvar) #string data type fruit = "apple" print (fruit) price = 1.50 print (price) #list data type list1 = [1,2,3,"a","b"]; print (list1[3]) a = 1 b = 5 c = a + b print (a + b) d = 4 e = d + a print (e)

VARIABLE DATA TYPES

Explain what is a Data Type. Data type is a classification of data which tells the compiler or interpreter how the programmer intends to use the data. Describe the following data types: Integer - It is used for whole number. String - It contains alphanumeric characters or text. Decimal - It contains decimal point. Float - Represent the real numbers that are approximated List - An abstract data type that represents a countable number of ordered values Boolean - To represent the truth values of logic. Example code for each data type. Integer - 10 String - 'Apple' Decimal - 13.2 Float - 0.0003 List - 1,2,3,4,5,6,7,8 Boolean - True/False, Yes/No En.wikipedia.org. (2017).  Data type . [online] Available at: https://en.wikipedia.org/wiki/Data_type [Accessed 10 Oct. 2017].

Pascal programming language

P ascal programming language Definition: Pascal is an imperative and procedural programming language, which Niklaus Wirth designed in 1968–69 and published in 1970, as a small, efficient language intended to encourage good programming practices using structured programming and data structuring. Pascal was developed on the pattern of the ALGOL 60 language. Wirth had already developed several improvements to this language as part of the ALGOL X proposals, but these were not accepted and Pascal was developed separately and released in 1970 En.wikipedia.org. (2017).  Pascal (programming language) . [online] Available at: https://en.wikipedia.org/wiki/Pascal_(programming_language) [Accessed 10 Oct. 2017]. Example: