DFN40323 – PROGRAMMING ESSENTIALS IN PYTHON TOPIC 1 1.3 Follow rules in assigning variables for Python 1.4 Construct Python literals
Learning Outcomes At the end of this topic, students will be able to: Identify rules in declaring Python variables Construct Python literals Identify Python function a. input () function b. String operators Manipulate Python literals using converting operation a. strings into numbers b. numbers to strings
Variables and Data Types in Python 01
Variable Variables are containers for storing data values. Python has no command for declaring a variable. A variable is created the moment you first assign a value to it. Variables do not need to be declared with any particular type and can even change type after they have been set.
Example of Variable A variable is created the moment you first assign a value to it. x = 2 y = “Python" print(x) print(y) Can change type after they have been set. z = 1 # z is of type int z = “happy" # z is now of type str print(z) Casting x = str(3) # x will be '3' y = int(3) # y will be 3 z = float(3) # z will be 3.0
Example of Variable Get the Type Use type() function Single or Double Quotes? String variables can be declared either by using single or double quotes. Case-Sensitive Variable names are case-sensitive. a = 4 A = "Sally" #A will not overwrite a x = "John" # is the same as x = 'John' x = 5 y = "John" print(type(x)) print(type(y))
—Variable Names A variable can have a short name (like x and y) or a more descriptive name (weight, studentname, area_square). Rules for Python variables: A variable name must start with a letter or the underscore character A variable name cannot start with a number A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)
Check the Variable Names myvar = "John" my_var = "John" _my_var = "John" myVar = "John" MYVAR = "John" myvar2 = "John" 2myvar = "John" my-var = "John" my var = "John"
Multi Words Variable Names Camel Case myVariableName = "John" Snake Case my_variable_name = "John" Pascal Case MyVariableName = "John"
Output Variables Example 1 x = "awesome" print("Python is " + x) Example 2 x = "Python is " y = "awesome" z = x + y print(z) The Python print statement is often used to output variables. To combine both text and a variable, Python uses the + character: Example 3 x =5 y =10 print(x + y) x = 5 y = "John" print(x + y)
Global Variables x = "awesome" def myfunc(): print("Python is " + x) myfunc() x = "awesome" def myfunc(): x = "fantastic" print("Python is " + x) myfunc() print("Python is " + x)
Input() Python allows for user input. Example: name = input("Enter your name:") print(Your name is: " +name)
String Operators Python allows several string operators that can be applied on the python string are as below: • Assignment operator: “=.” • Concatenate operator: “+.” • String repetition operator: “*.” • String slicing operator: “[]” • String comparison operator: “==” & “!=” • Membership operator: “in” & “not in” • Escape sequence operator: “\.” • String formatting operator: “%” & “{}” https://www.educba.com/string-operators-in-python/
Data Types Data types are the classification or categorization of data items. Python supports the following built-in data types:
Check the Data Types print(type(x))
Complete the exercises: https:/ / www.w3schools.com/ python/ python_variabl es_exercises.asp https:/ / www.w3schools.com/ python/ python_dataty pes.asp
Operators and Operators Precedence 02
Learning Outcomes At the end of this topic, students will be able to: Explain variables and data types in Python Apply Operators and Operators Precedence Define and write a Conditional and Looping Process ordered and unordered data structure Apply common Python program elements
Operators Operators are used to perform operations on variables and values. Example: print(3 + 5) a = 2, b=1 print(a * b)
Python Arithmetic Operators Operator Operation + Addition - Subtraction * Multiplication / Division ** Power % Remainder // Floor division
Example: x = 13 y = 3 print(x / y) Output: 4.333333333333333 Division (/)
Remainder (%) Example: x = 23 y = 5 print(x % y) Output : 3 5 23 20 3 4
Example: x = 17 y = 3 print(x // y) Output: 5 Floor Division (//)
>>> xx = 2 >>> xx = xx + 2 >>> print(xx) 4 >>> yy = 440 * 12 >>> print(yy) 5280 >>> zz = yy / 1000 >>> print(zz) 5.28 >>> jj = 23 >>> kk = jj % 5 >>> print(kk) 3 >>> print(4 ** 3) 64 Example
Python Assignment Operators Assignment operators are used to assign values to variables: https://data-flair.training/blogs/python-operator/
Python Comparison Operators Comparison operators are used to compare two values:
Python Logical Operators Logical operators are used to combine conditional statements:
Operators Precedence Highest precedence rule to lowest precedence rule: 1. Parentheses are always respected 2. Exponentiation (raise to a power) 3. Multiplication, Division, and Remainder 4. Addition and Subtraction 5. Left to right Parenthesis ( ) Power Multiplication Addition Left to Right
The Complete Operators Precedence
Do the exercises: 1. https:/ / www.w3schools.com/ python/ exercise.asp?fil ename=exercise_operators1
Exercise - Predict the output: w = 20 x = 10 y = 15 z = 2 result_1 = (w+x)*y/z 225 result_2 = ((w+x)*x)/z 150 result_3 = ((w+x)*(y/z))**z 50625 result_4 = w+(x*y)/z 95 print("The value of (w+x)* y/z is',result_1) print('The value of ((w+x)*x)/z is',result_2) print('The value of ((w+x)*(y/z))**z is',result_3) print('The value of w+(x*y)/z is',result_4)
Exercise - Predict the output: age = int(input('Please enter your age in years: ')) income = int(input('Please enter your annual income: ')) if age >= 18 and income >=15000: print('You can apply for a loan') else: print('You CANNOT apply for a loan')
https:/ / www.w3schools.com/ python/ python_variables.asp https:/ / www.tutorialsteacher.com/ python/ python-data-types https:/ / data-flair.training/ blogs/ python-operator/ https:/ / realpython.com/ python-conditional-statements/ E-book : Python for Everybody https:/ / www.py4e.com/ Resources
CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik. Thanks!