Python Programming Basics Lecture
Python Programming Basics Lecture
Programing in Python
                      Instructor : AALWAHAB DHULFIQAR   Advisor : Dr. Tejfel Mate
                                                                  if the_weather_is_good:
                                                                     go_for_a_walk()
if true_or_false_condition:
                                                                  elif tickets_are_available:
   perform_if_condition_true
                                                                     go_to_the_theater()
else:
                                                                  elif table_is_available:
   perform_if_condition_false
                                                                     go_for_lunch()
                                if the_weather_is_good:
                                                                  else:
if the_weather_is_good:            if nice_restaurant_is_found:
                                                                     play_chess_at_home()
   go_for_a_walk()                    have_lunch()
else:                              else:
   go_to_a_theater()                  eat_a_sandwich()
have_lunch()                    else:
                                   if tickets_are_available:
if the_weather_is_good:               go_to_the_theater()
   go_for_a_walk()                 else:
   have_fun()                         go_shopping()
else:
   go_to_a_theater()
   enjoy_the_movie()
have_lunch()
Pseudocode and introduction to loops                            An infinite loop
i=0                                                                   break - exits the loop immediately, and unconditionally ends the loop's
while i < 100:                                                        operation; the program begins to execute the nearest instruction after
   # do_something()                                                   the loop's body;
   i += 1                                                             continue - behaves as if the program has suddenly reached the end of
--------------------------------------------------                    the body; the next turn is started and the condition expression is tested
for i in range(100):                                                  immediately.
   # do_something()
   pass                                                                                                    # break - example
                                     Argument A               Argument B                 A or B
for i in range(5):                     False                     False                   False
  print(i)                             False                     True                    True
else:                   0
                                        True                     False                   True
  print("else:", i)     1
                        2               True                     True                    True
i = 111                 3
for i in range(2, 1):   4
   print(i)             else: 4
else:                   else: 111              Argument                    not Argument
   print("else:", i)                              False                        True
                                                  True                        False
Logical expressions
Bitwise operators
x=x&y                     x &= y
                                                x ^ 1 = ~x
x=x|y                     x |= y
                                                x^0=x
x=x^y                     x ^= y
                                                              Binary left shift and binary right shift
                                                         var = 17
                                                         var_right = var >> 1
                                                         var_left = var << 2
                                                         print(var, var_left, var_right)
                                                     The len() function
Why do we need lists?
var1 = int(input())                                  The length of a list may vary during execution. New elements may be added to the list, while
var2 = int(input())                                  others may be removed from it. This means that the list is a very dynamic entity.
var3 = int(input())
                                                     If you want to check the list's current length, you can use a function named len() (its name
var4 = int(input())                                  comes from length).
var5 = int(input())
var6 = int(input())                                  The function takes the list's name as an argument, and returns the number of elements currently
                                                     stored inside the list (in other words - the list's length).
:
:
list is a collection of elements, but each
element is a scalar.
numbers = [10, 5, 7, 2, 1]
How do you change the value of a chosen element in the list? Negative indices are legal
print(5 in my_list)
print(5 not in my_list)                                               EMPTY = "-"
print(12 in my_list)                                                  ROOK = "ROOK"
                                                                      board = []
                                                                      for i in range(8):
Lists in lists                                                          row = [EMPTY for i in range(8)]
                                                                        board.append(row)
row = []                                                              board[0][0] = ROOK
for i in range(8):                                                    board[0][7] = ROOK
  row.append(‘_____’)           row = ["-------" for i in range(8)]   board[7][0] = ROOK
print (row)                                                           board[7][7] = ROOK
                                                                      print(board)
                                                                      for i in board:
List comprehensive                                                      print (i)
Decomposition
def function_name():
  function_body
def message():
  print("Enter a value: ")
How functions work
                                                                                   Donot do these:
                                                              def message():
                                                                print("Enter a value: ")
                                                              def message():
                                                                print("Enter a value: ")
message = 1
Parameterized functions
 def function(parameter):
                                      def message(number):
   ###
                                        print("Enter a number:", number)
 def message(number):
                                      message()
   print("Enter a number:", number)
Parametrized functions:                     Positional parameter passing
                                             def my_function(a, b, c):
                                               print(a, b, c)
my_function(1, 2, 3)
                                             introduction("Luke", "Skywalker")
                                             introduction("Jesse", "Quick")
 def message(what, number):                  introduction("Clark", "Kent")
   print("Enter", what, "number", number)
1+2+3=6 2+3+1=6
adding(3, c = 1, b = 2) 3+2+1=6
adding(3, a = 1, b = 2) 3+2+1=6
global name
global name1, name2, ...
                                           def fib(n):
                                             if n < 1:
                                                return None
                                             if n < 3:
def my_function():                              return 1
  global var
  var = 2                                    elem_1 = elem_2 = 1
  print("Do I know that variable?", var)     the_sum = 0
                                             for i in range(3, n + 1):
                                               the_sum = elem_1 + elem_2
var = 1                                        elem_1, elem_2 = elem_2, the_sum
my_function()                                return the_sum
print(var)