Python Programming
Python Programming
Python Programming
PROGRAMMING
Python Installation
• Output Variables
• The Python print() function is often used to output variables.
• x = "Python is awesome"
print(x)
• In the print() function, you output multiple variables, separated by a
comma:
• x = "Python"
• y = "is"
• z = "awesome"
• print(x, y, z)
• You can also use the + operator to output multiple variables:
• x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
• Notice the space character after "Python " and "is ", without them the
result would be "Pythonisawesome".
• For numbers, the + character works as a mathematical operator:
•x = 5
y = 10
print(x + y)
• In the print() function, when you try to combine a string and a
number with the + operator, Python will give you an error:
•x = 5
y = "John"
print(x + y)
• The best way to output multiple variables in the print() function is to
separate them with commas, which even support different data
types:
•x = 5
y = "John"
print(x, y)
Built-in Data Types
• print("Hello")
• print('Hello')
• a = "Hello"
• print(a)
• Multiline Strings
• a = """Lorem ipsum dolor sit amet,
• consectetur adipiscing elit,
• sed do eiusmod tempor incididunt
• ut labore et dolore magna aliqua."""
• print(a)
• Or three single quotes:
• a = '''Lorem ipsum dolor sit amet,
• consectetur adipiscing elit,
• sed do eiusmod tempor incididunt
• ut labore et dolore magna aliqua.'''
• print(a)
• String Concatenation
• a = "Hello"
• b = "World"
•c=a+b
• print(c)
• a = "Hello"
• b = "World"
• c=a+""+b
• print(c)
Python Booleans
• You can evaluate any expression in Python, and get one of two answers,
True or False.
• When you compare two values, the expression is evaluated and Python
returns the Boolean answer:
• print(10 > 9)
• print(10 == 9)
• print(10 < 9)
PYTHON OPERATORS
• Python Arithmetic Operators
• x=5
• y=3
• print(x + y)
• x=5
• y=3
• print(x - y)
• x=5
• y=3
• print(x * y)
• x = 12
• y=3
• print(x / y)
• x=5
• y=2
• print(x % y)
• x=2
• y=5
• print(x ** y) #same as 2*2*2*2*2
• x = 15
• y=2
• print(x // y)
• #the floor division // rounds the result down to the nearest whole
number
• x=5
• print(x)
• x=5
• x += 3
• print(x)
• x=5
• x -= 3
• print(x)
• x=5
• x -= 3
• print(x)
• x=5
• x *= 3
• print(x)
• x=5
• x *= 3
• print(x)
• x=5
• x /= 3
• print(x)
• x=5
• x%=3
• print(x)
• x=5
• x//=3
• print(x)
• x=5
• x **= 3
• print(x)
• x=5
• y=3
• print(x == y)
• # returns False because 5 is not equal to 3
• x=5
• y=3
• print(x != y)
• x=5
• y=3
• print(x < y)
• # returns False because 5 is not less than 3
• x=5
• y=3
• print(x < y)
• # returns False because 5 is not less than 3
• x=5
• y=3
• print(x >= y)
• # returns True because five is greater, or equal, to 3
• x=5
• y=3
• print(x <= y)
• # returns False because 5 is neither less than or equal to 3
• x=5
• print(x > 3 and x < 10)
• # returns True because 5 is greater than 3 AND 5 is less than 10
• x=5
• print(x > 3 or x < 4)
• # returns True because one of the conditions are true (5 is greater
than 3, but 5 is not less than 4)
• x=5
• print(not(x > 3 and x < 10))
• # returns False because not is used to reverse the result
Operator Precedence
• Operator Precedence
• Operator precedence describes the order in which
operations are performed.
• print((6 + 3) - (6 + 3))
• """
• Parenthesis have the highest precedence, and need to be evaluated
first.
• The calculation above reads 9 - 9 = 0
• """
• print(100 + 5 * 3)
• """
• Multiplication has higher precedence than addition, and needs to be
evaluated first.
• The calculation above reads 100 + 15 = 115
• “””
• If two operators have the same precedence, the
expression is evaluated from left to right.
• Example
• Addition + and subtraction - has the same precedence, and therefor
we evaluate the expression from left to right:
• print(5 + 4 - 7 + 3)
• """
• Additions and subtractions have the same precedence, and we need
to calculate from left to right.
• The calculation above reads:
• 5+4=9
• 9-7=2
• 2+3=5
• """
Membership IN Operator
• Membership in operator explanation with example
• The in operator in Python is used to test whether a value exists in a
sequence (such as a string, list, or tuple).
• It returns True if the value is found, and False otherwise.
• # Using the 'in' operator with a list
• fruits = ['apple', 'banana', 'orange']
• Example
• Clear the list content:
• # Accessing elements
• print("Tuple:", my_tuple)
• print("First element:", my_tuple[0])
• print("Second element:", my_tuple[1])
• # Tuple unpacking
• a, b, c, d = my_tuple
• print("Unpacked elements:", a, b, c, d)
• # Repeating elements in a tuple
• repeated_tuple = ('hello',) * 3
• print("Repeated tuple:", repeated_tuple)
• # Slicing a tuple
• sliced_tuple = my_tuple[1:3]
• print("Sliced tuple:", sliced_tuple)
• # Nested tuple
• nested_tuple = ((1, 2), ('a', 'b'), (True, False))
• print("Nested tuple:", nested_tuple)
• print(my_list)
• Tuples:
• Tuples are immutable, meaning once they are created, you cannot
modify their contents (add, remove, or change elements).
• Tuples are defined using parentheses ().
• # Creating a tuple
• my_tuple = (1, 2, 3, 4, 5)
• # Attempting to modify the tuple will raise an error
• # my_tuple[2] = 10 # This will result in an error
• # Tuples do not have append or remove methods
• print(my_tuple)
Example:Tuple with various aspects
• # Creating a tuple
• my_tuple = (1, 'apple', 3.14, True)
• # Accessing elements
• print("Tuple:", my_tuple)
• print("First element:", my_tuple[0])
• print("Second element:", my_tuple[1])
• # Tuple unpacking
• a, b, c, d = my_tuple
• print("Unpacked elements:", a, b, c, d)
• # Concatenating tuples
• tuple1 = (1, 2, 3)
• tuple2 = ('a', 'b', 'c')
• concatenated_tuple = tuple1 + tuple2
• print("Concatenated tuple:", concatenated_tuple)
• # Nested tuple
• nested_tuple = ((1, 2), ('a', 'b'), (True, False))
• print("Nested tuple:", nested_tuple)