compile ICT.pdf
Document Details
Uploaded by TemptingRuthenium2267
Full Transcript
PYTHON Key stage IV Class IX དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག། Department of School Education Ministry of Education & Skills Development Python Coding...
PYTHON Key stage IV Class IX དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག། Department of School Education Ministry of Education & Skills Development Python Coding Class IX ICT Curriculum January 2024 January 2024 © ICT Curriculum, MoESD PYTHON Key stage IV Class IX Key Concept # 3 INTRODUCTION TO PYTHON January 2024 © ICT Curriculum, MoESD PYTHON Key stage IV Class IX Concept Overview Coding concept Sub - Concepts Background of Python Advantages of Python Career Scope of Python Introduction to Python Python installation Introduction to IDLE ○ Interactive mode ○ Script mode January 2024 © ICT Curriculum, MoESD PYTHON Key stage IV Class IX Objectives Define programming with examples. Explain the features of Python programming. Identify the benefits of learning Python programming language. List the career opportunities associated with Python. Familiarize with Python text editor (IDLE). Write Python programs in interactive and script mode. January 2024 PYTHON Key stage IV Class IX Computer Programming Process of writing set of instructions to solve the real life problem Computer programming is a set of instructions that tells a computer to perform a specific task. Every programming languages has its own set of rules that dictate how programs should be written known as syntax. January 2024 PYTHON Key stage IV Class IX Computer Programming (cont.) Just like humans use various languages to communicate, different computer languages are used in programming. Some of the common computer programming languages are: Python January 2024 PYTHON Key stage IV Class IX ACTIVITIES 1. Understanding the origin of Python PYTHON 2. Demo 1 - Python Installation 3. Demo 2 - Creating Python file 4. Demo 3 - Saving and executing Python program 5. Demo 4 - Opening Python file 6. Activity 1 - Application of Python January 2024 PYTHON Key stage IV Class IX Python Python was developed by Guido Van Rossum in 1989 while working at National Research Institute in Netherland but officially, Python was made available to the public in 1991. The name Python was selected from the TV Show "The Complete Monty Python's Circus", which was broadcast on the BBC from 1969 to 1974. Guido Van Rossum File extension of Python program is.py Note:Python is recommended as the first programming language for beginners as it is simple and easy to learn. The icon of Python January 2024 PYTHON Key stage IV Class IX Features of Python 1. Free and Open- Python is completely free to use, even for Source commercial purposes. 2. Cross-platform Python can run on different operating systems compatibility such as Windows, macOS, and Linux Python source code is executed line by line, 3. Interpreted language and not all at once. Python allows variables to hold values of 4. Dynamically typed any data type, and their types can be changed dynamically during runtime. January 2024 PYTHON Key stage IV Class IX Features of Python Examples 5. Code Optimization: Python allows code to be written with fewer lines than some other programming languages. Python takes only one line to display “hello world” compared to C, C++ and Java. January 2024 PYTHON Key stage IV Class IX Career Opportunities with Python Some of the common career opportunities with Python are: Data Analyst Python helps analysts in collecting, processing, and visualizing diverse data for informed decision-making. Web Developer Python is used by web developers to build dynamic websites and applications. January 2024 PYTHON Key stage IV Class IX Career Opportunities with Python (cont.) Machine Learning Engineer Python is used to build models for data-driven predictions, enabling machines to learn autonomously and improve their performance over time. Artificial Intelligence Developer Python is used to develop computer systems such as Artificial Intelligence that are capable of performing tasks that typically require human intelligence. January 2024 PYTHON Key stage IV Class IX Python Text Editor A text editor in programming is a software tool designed for creating and editing plain text often used for writing code. Some of the text editors to write the Python code are: In classes IX and X Python IDLE will be used to write Python programs. January 2024 PYTHON Key stage IV Class IX Python IDLE Development Learning Integrated IDLE Environment In general we need to install text editor to write codes. When we install Python software, IDLE editor is installed as a default editor. IDLE is Python's development environment used to write and run Python Programs. January 2024 PYTHON Key stage IV Class IX Demo 1 - Python Installation 1. Visit the Python official website at python.org 2. Click on the download link to start the download. 3. In the Python Installer, make sure to check the box that says "Add Python x.x to PATH." 4. Click on the finish button to complete the installation. January 2024 PYTHON Key stage IV Class IX Understanding IDLE There are two types of IDLE mode: 1. Interactive mode/ Shell 2. Script/ Editor mode 1. Interactive Mode is used for the following purposes: 1 Exploring modules and libraries 2 Quick Testing and Experimentation 3 Allow only a line of code January 2024 PYTHON Key stage IV Class IX Understanding IDLE 2. Script Mode is used for the following purposes: 1 Creating larger program 2 Code reusability 3 Editing and saving code 4 To execute entire codes at once January 2024 PYTHON Key stage IV Class IX Demo 2 - Creating a Python File Given below are the steps to display the message “Hello Bhutan” in Python script mode. 1. Open IDLE 2. Click on File tab 3. Select New File 4. Write a code to display message “Hello Bhutan”. January 2024 PYTHON Key stage IV Class IX Demo 3 - Saving and Executing Python program Given below are the steps to save and execute a Python program. 1. Click on File tab. 2. Select Save option. 3. Select the destination folder. 4. Give file name. 5. Click ok. 1. Click on run Tab. 2. Select run module option or F5. January 2024 PYTHON Key stage IV Class IX Demo 4 - Opening a Python File Given below are the steps to open an existing Python file. 1. Select the Python file you want to open. 2. Right click on it and hover over “Edit with IDLE”. Then select “Edit with IDLE 3.x.x”. January 2024 PYTHON Key stage IV Class IX Activity 1 - Application of Python 1 2 Open Python IDLE in both the interactive Browse the Internet to get a and script mode, and find out the deeper understanding of difference between them. Which mode Python programming and would you use to create a new program in share at least two points with Python? the class. 3 4 Make a list of applications or Write a Python program to display the websites that are created using message ”The best way to predict your Python. Share the applications future is to create it-Abraham Lincoln” in to the class. the interactive mode and script mode. January 2024 PYTHON Key stage IV Class IX Activity 2 - Multiple Choice Questions 1. Python is known for its ___________. A. Complex syntax B. Simplicity and readability C. Limited applications D. Lack of community support 2. Every programming language has its own set of rules to give instructions to the computer. These rules are called___________. A. Syntax B. Guidelines C. Structure D. Format January 2024 PYTHON Key stage IV Class IX Activity 2 - Multiple Choice Questions(cont.) 3. To instruct the computer system we use_______. A. English B. Human language C. Programming language D. All of the above 4. What is the file extension for Python source code files? A..pyt B..pyth C..p D..py January 2024 PYTHON Key stage IV Class IX Activity 2 - Multiple Choice Questions(cont.) 5. Why is Python IDLE considered a good choice for beginners in Python programming? A. It offers advanced features suitable for large projects. B. It comes bundled with Python, requiring no additional installations. C. It provides a complex interface for professional developers. D. It is exclusively designed for web development. January 2024 PYTHON Key stage IV Class IX Key Points Programming is a process of creating a set of instructions that tell a computer how to perform a task. Python, JavaScript, Java, C++, C are some examples of Programming language. Python has a simple syntax compared to other programming languages. Every programming language has its own set of rules and structure to be followed while programming known as Syntax. January 2024 PYTHON Key stage IV Class IX Key Points Python IDLE is used to write and execute the Python program. Python IDLE is divided into 2 types: Shell or Interactive Interpreter and Script or editor. Interactive mode is designed for quick testing and exploration, while script mode is used for creating more substantial, reusable programs, and projects..py is the extension of Python program. January 2024 PYTHON Key stage IV Class IX དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག། Department of School Education Ministry of Education & Skills Development Python Coding Class IX ICT Curriculum January 2024 January 2024 PYTHON Key stage IV Class IX Key Concept # 5 OUTPUT January 2024 PYTHON Key stage IV Class IX Concept Overview Coding Concept Sub - Concepts print( ) function Output formatting ○ Comma operator ( , ) Output ○ Formatting with + Operator ○ Formatted strings (f-strings) January 2024 PYTHON Key stage IV Class IX Objectives Explain the syntax of the print( ) function. List the different types of output formatting such as formatting using Comma operator ( , ), Formatting with + Operator, Formatting strings (f- strings). Differentiate among the different types of output formatting. Display text, numbers and characters using print( ) function. January 2024 PYTHON Key stage IV Class IX ACTIVITIES 1. Understanding Output formatting using print() 2. Demo 1 - Displaying Dorji’s five cousins’ name 3. Activity 1 - Printing Kuzuzangpo La and OUTPUT Tree 4. Activity 2 - Printing Output 5. Activity 3 - Creating a Man with Signboard 6. Activity 4 - Displaying Student Biodata 7. Activity 5 - Test Your Understanding January 2024 PYTHON Key stage IV Class IX Output Output refers to information or a result produced by a computer after taking an input, processing it, storing it, and displaying it as an output on the screen. January 2024 PYTHON Key stage IV Class IX Print Function In Python, print() function is used to display a specific message on a Python console or terminal. Syntax print(object,sep="",end="") 1. Object is mandatory but sep and end parameters are optional. 2. If object is the string data type, it should be enclosed within single or double quotes[" " OR ' ']. 3. If an object is a variable name, there is no need to enclose it inside the quotation mark. January 2024 PYTHON Key stage IV Class IX Examples Printing special characters Code: Printing print(“=^ ^=”) numeric values Output: Code: =^ ^= print(2024) Printing text Output: Code: 2024 print("Hello World!") Output: Hello World! January 2024 PYTHON Key stage IV Class IX Output Formatting It refers to presenting the output in a desired format that is readable and meaningful to the user. In order to format the output, it is necessary to combine different values and create the desired output. Basic output formatting can be achieved by using the following: 1 2 3 Comma operator ( , ) Plus operator(+) F-strings( f’{}’ ) January 2024 PYTHON Key stage IV Class IX Output Formatting (cont.) Code 1 1 print(“Bhutan”,”India”,”USA”) Output Comma operator ( , ) Bhutan India USA Separate multiple objects to be Code printed in the same line 1 print(0, 1.1, -200, 3000) Output 0 1.1 -200 3000 January 2024 PYTHON Key stage IV Class IX Output Formatting (cont.) Code -Example 2 1 print("Hello " + "Pema") Output Plus operator ( + ) Hello Pema join or combine more than one Code -Example object or variable 1 print("Age: " + "17") name(Concatenate strings) Output Age: 17 January 2024 PYTHON Key stage IV Class IX Output Code -Example Formatting (cont.) 1 score = 50 2 print(f’{score}points’) Output 3 50 points F-strings( f’{}’ ) Code -Example add expressions 1 Name = "Pema" and variables 2 print(f'Hello {Name}') directly into strings. Output Hello Pema January 2024 PYTHON Key stage IV Class IX Demo 1 - Displaying Dorji’s five cousin name Dorji has five cousin brothers namely Karma Tshering, Pema Dorji, Kummar Galay, Tshewang Tenzin and Samphel Rabten. Write a Python program to display the names of all the cousin brothers of Dorji. Code Output 1 print(‘*************') ************* 2 print(' Cousin ') Cousin 3 print('*************') ************* 4 print('Karma Tshering') Karma Tshering 5 print('Pema Tshering') Pema Tshering 6 print('Kummar Galay') Kummar Galay 7 print('Tshewang Tenzin') Tshewang Tenzin 8 print('Samphel Rabten') Samphel Rabten January 2024 PYTHON Key stage IV Class IX Activity 1 - Printing Kuzuzangpo La and Tree 1. Write a Python program to print 1 Code “Kuzuzangpo La”. 1 print('Kuzuzangpo La') 2. Write a Python program to print a cypress tree pattern as shown below. 2 Code 1 print(' *') 2 print(' ***') 3 print(' *****') 4 print(' *******') 5 print(' | |') January 2024 PYTHON Key stage IV Class IX Activity 2 - Printing Output Copy the code in your notebook and write the output for each. 1 Code 2 Code 1 name=”Pema” 1 print("Hello", "how are 2 age=16 you?") 3 print(f'My name is {name} and I am {age} Output years old.') Hello how are you? 2 Output 1 My name is Pema and I am 16 years old. January 2024 PYTHON Key stage IV Class IX Activity 3 - Creating a man with Signboard Write a Python program to create a robot holding a signboard with ‘Kuzuzangpo la’ message displayed on it. January 2024 PYTHON Key stage IV Class IX Activity 3 Solution Code 1 print(' \||||||||||/ ') 2 print(' 0 0 ') 3 print(' ___oo0_____( )____________') 4 print('| |') 5 print('| |') 6 print('| "Kuzuzangpo la" |') 7 print('| |') 8 print('|__________________oo0____|') 9 print(' | | | |') 10 print(' | | | |') 11 print(' o0|_| |_|0o') January 2024 PYTHON Key stage IV Class IX Activity 4 - Displaying Student Biodata Write a Python program to display a students Biodata. Code 1 print(“Name: Rigzin Samdrup”) 2 print(“Age: 20”) 3 print(“DOB: 04/04/2021”) 4 print(“Class: IX”) 5 print(“School: Mongar Higher Secondary School”) 6 print(“Dzongkhag: Mongar”) January 2024 PYTHON Key stage IV Class IX Activity 5 - Test your understanding. Answer the following questions by selecting the best answer from the four options given. 1. What is the primary purpose of the print() function in Python? A) Define a new variable B) Input data from the user C) Mathematical calculations D) Display output to the console 2. Which of the following statements is used for printing multiple values on the same line in Python? A) print(value1; value2) B) print(value1, value2) C) print(value1 + value2) D) print(value1 \n value2) January 2024 PYTHON Key stage IV Class IX Key Points print() function can be used to display a specific message on a Python console or terminal. Output formatting can be used to present the output in a desired format that is readable and meaningful to the user We can print multiple values on the same line using comma separator. F-string can be used to format expressions within strings. Plus operator ( + ) can be used to display output by concatenating strings. January 2024 PYTHON Key stage IV Class IX དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག། Department of School Education Ministry of Education & Skills Development Python Coding Class IX ICT Curriculum January 2024 January 2024 PYTHON Key stage IV Class IX Key Concept # 6 VARIABLES January 2024 PYTHON Key stage IV Class IX Concept Overview Coding Concept Sub - Concepts Definition of variables Importance of variables Variable naming convention Variables Types of variable assignment ○ Direct assignment ○ Multiple assignments ○ Chain assignment January 2024 PYTHON Key stage IV Class IX Objectives Define variable with examples. Create variables to store different types of data. Differentiate between different ways of assigning values to variables. Explain rules for creating a variable. Create a program using the different types of variable assignments. January 2024 PYTHON Key stage IV Class IX ACTIVITIES 1. Understanding Variables 2. Demo 1 - Variable Naming Conventions 3. Demo 2 - Multiple Assignment VARIABLE 4. Activity 1 - Identifying Variable Names 5. Demo 3 - Multiple assignment 6. Activity 2 - Variable Manipulation January 2024 PYTHON Key stage IV Class IX Variables Variables are containers for storing data values such as numbers, strings, lists, sets, tuples, or dictionaries. Example Assignment Operator A variable must have a age = 15 assignment operator(=) followed by a value. Variable Value January 2024 PYTHON Key stage IV Class IX Examples Variables myInt=4 myReal=2.5 myChar=”a” myString=”hello” Values January 2024 PYTHON Key stage IV Class IX Importance of Variable variables can hold values like numbers, strings, Data Storage lists, set, tuple and dict. Variables can be used in operations to Data Manipulation manipulate the stored data. Variables allow data to be shared between Value Sharing different parts of a program. Variables provide descriptive names for the Data Representation stored data. January 2024 PYTHON Key stage IV Class IX Rules for Creating a Variable Name We must follow rules when creating variables in Python. Following are the rules used in naming a variable. Reserved keywords cannot be used as a variable Variable names cannot name. contain spaces or special Variable name must start Example: print, for, in, and, characters, except with a letter (a-z, A-Z) or an or, etc underscores (_). underscore (_). Example: first name, Example: name, _name name#123 Variable Variable names are case- A variable name cannot naming sensitive. start with a numeric value. Example: age and Age are Example: 89age, 123name. different. January 2024 PYTHON Key stage IV Class IX Demo 1 - Naming Variables Given below is an example to show the correct and incorrect ways of naming variables. Correct variable names Incorrect variable names 1 fullname2=”Karma Deki” 2fullname=”Karma Deki” 2 fullname=”Sonam Dorji” full-name=”Sonam Dorji” 3 full_name=”Gakpa Samdrup” Full name=”Gakpa Samdrup” 4 fullName=”Chimi Wangmo” $_fullname=”Chimi Wangmo” 5 Fullname=”Pelden Dorji” full_#_name=”Pelden Dorji” 6 _fullname=”Tshewang Tenzin” Full n@me=”Tshewang Tenzin” January 2024 PYTHON Key stage IV Class IX Demo 2 - Good and Bad Variable Names All the variables listed below are declared correctly, but it shows the examples of both good and bad variable naming. Variable name should be descriptive so that it is clear, what the variable represents. Good variable name declaration Bad variable name declaration 1. user_name 1. username 2. product_price 2. x 3. current_year 3. num 4. number_of_students 4. a January 2024 PYTHON Key stage IV Class IX Activity 1 - Identifying Variable Names Study the codes provided below carefully and list down the variable names used in the program in your notebook. Also write the output of the program. Code Variables used in the codes are: 1 Name=”Pema” 1. Name 2 Age = 16 2. Age 3 print(“my name is”,Name) Output 4 print(“my age is”, Age) My name is Pema My age is 16 January 2024 PYTHON Key stage IV Class IX Different ways to assign values to variables There are various ways to assign values to variable. Some of the commonly used variable assignment in Python are: 1 Direct assignment: Assign a single value to a single variable. Example: Dzongkhag = “Trashigang” 2 Chained assignment: Assign a single value to multiple variables. Example: x = y = z = 20 3 Multiple assignment: Assign multiple values to multiple variables. Example: name, age, class = “Kinley Wangmo”, 32, 10 January 2024 PYTHON Key stage IV Class IX Demo 3 - Multiple assignment Write a Python program to display information about a student named Devi Gurung, 14 years old from Kangpar LSS. Use multiple assignment variable. Solution 1 name,age,school=”Devi Gurung”,14,”Kangpar LSS” 2 print(“Student information”) Output 3 print(“Name:”,name) Student information 4 print(“Age:”,age) Name:Devi Gurung Age:14 5 print(“School:”,school) School: Kangpar LSS January 2024 PYTHON Key stage IV Class IX Activity 2 - Variable Manipulation 1. Divide yourselves into four groups. 2. Each group will receive a question. 3. In respective groups read the question carefully before discussing the solution. 4. For the solution: 5. Workout the solution in your notebook. 6. Write the code in Python IDLE. 7. Run the program. 8. Check the output and make necessary changes. January 2024 PYTHON Key stage IV Class IX Questions for the group activity 1 2 3 4 Create a variable Create two Create two Create two variables, variables, "num1" variables, "x" and “count” and assign "first_name" and and "num2", and the value 10. "y", and assign them "last_name” using assign them with Update the value appropriate values. values of 5 and 10, multiple Compute the sum of respectively. Print of the “count” to be assignments. Assign them with relevant num1 and num2 and the numbers. Then twice its current values and then use store the result in a swap the values of x value. Then print variable named the print function to and y using a the new value. "sum_result". Print display the full swapping technique. the value of name. Print the updated sum_result. values of x and y. January 2024 PYTHON Key stage IV Class IX Activity 1 Solution for Group 1 Create a variable “count” and assign the value 10. Update the value of the “count” to be twice its current value. Then print the new value. Solution 1 count = 10 Output 2 count1 = 2*count Count:20 3 print(“count:”,count1) January 2024 PYTHON Key stage IV Class IX Activity 1 Solution for Group 2 Create two variables, "first_name" and "last_name” using multiple assignments. Assign them with relevant values and then use the print function to display the full name. Solution 1 #program to create a variable and store data value. 2 first_name, last_name = ”Tshering”,”Dema” 3 print(f’My name is {first_name} {second_name}’) Output My name is Tshering Dema January 2024 PYTHON Key stage IV Class IX Activity 1 Solution for Group 3 Create two variables, "num1" and "num2", and assign them with appropriate values. Compute the sum of num1 and num2 and store the result in a variable named "sum_result". Print the value of sum_result. Solution 1 num1, num2 = 7,3 2 sum_result = num1 + num2 3 print(“Result:”,sum_result) Output Result:10 January 2024 PYTHON Key stage IV Class IX Activity 1 Solution for Group 4 Create two variables, "x" and "y", and assign them values of 5 and 10, respectively. Print the numbers. Then swap the values of x and y using a swapping technique. Print the updated values of x and y. Solution 1 x = 5 2 y = 10 3 print(f’Before x = {x}, y = {y}’) 4 #Before x=5 and y=10 Output 5 temp = x 6 x = y Before x = 5, y = 10 7 y = temp After x = 10, y = 5 8 print(f’After x = {x}, y = {y}’) January 2024 PYTHON Key stage IV Class IX Key Points Variables in programming serve as containers for data storage. It is important to use variable in programming as it helps in data storage, data manipulation, value sharing and data representation. We must follow the variable naming convention while creating a variable. Variable name should be descriptive so that it is clear, what the variable represents. There are different ways to These are the types of variables assignment in Python. ○ Direct assignment ○ Multiple assignment, eg: x = y = 4. ○ Chained assignment, eg: x, y, z = 3, 4, 5 January 2024 PYTHON Key stage IV Class IX དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག། Department of School Education Ministry of Education & Skills Development Python Coding Class IX ICT Curriculum January 2024 January 2024 PYTHON Key stage IV Class IX Key Concept # 7 COMMENTS AND INPUT( ) January 2024 PYTHON Key stage IV Class IX Concept Overview Coding Concept Sub - Concepts Comments ○ Single line comment Comments & Input( ) ○ Multi-line comment Input function January 2024 PYTHON Key stage IV Class IX Objectives Explain comments with examples. Differentiate between single-line and multi-line comments. Explain the benefits of using comments for effective coding. Write comments in Python programs to enhance code readability. Define input function with example. Explain the purpose of the input( ) function. Create interactive programs using input() function. January 2024 PYTHON Key stage IV Class IX ACTIVITIES 1. Understanding Comments 2. Demo 1 - Single Comments COMMENTS 3. Demo 2- Multi-line Comments 4. Activity 1 - Displaying Message 5. Activity 2 - Writing Single and Multi-line Comments January 2024 PYTHON Key stage IV Class IX Comments Comments in coding are lines of text added to explain what the code does. Comments are not executed as part of the program; they are for human readers. Comments make the code easier to understand by providing explanations, notes, or reminders. Comments help other programmers who might need to work with the code later. Comments are like little messages written directly into the code for better comprehension. January 2024 PYTHON Key stage IV Class IX Benefits of Using Comments 1 Code Explanation Helps in adding note to explain the code. 2 Readability Enhances the code readability Helps team members to understand 3 Collaboration each other's code 4 Debugging Comments assist in debugging process 5 Documentation Gives Instruction guide in program 6 Learning and Teaching Makes it easier to understand the code January 2024 PYTHON Key stage IV Class IX Types of Comments There are two types of comments: Comments 1 2 Single line Multi-line comment comment January 2024 PYTHON Key stage IV Class IX Single-line Comments Single-line comments are used for Example brief explanations on the same line 1 #code below adds two numbers as the code. 2 x,y= 4,5 3 print(x+y) Single-line comments start with the # [Hash tag] symbol and In the above example, the text “code everything after it on the same line below adds two numbers” is a single is considered the comment. line comment as it is written after the # symbol and is also on the same line. January 2024 PYTHON Key stage IV Class IX Demo - Single-line Comments Code 1 #variable name stores string value Sonam 2 name = “Sonam” 3 #variable age is assigned with the integer value 16 4 age = 16 5 #Displaying the values stored in the variables 6 print(name) 7 print(age) Output 1 Sonam 2 16 January 2024 PYTHON Key stage IV Class IX Multi-line Comments Example Multi-line comments are enclosed 1 ’’’The code below within triple quotes (""") and can 2 initialises 3 variable x and y with the span across multiple lines. 4 value 4 and 5 respectively. 5 Lastly the sum of value 6 stored in x and y variable Multi-line comments are often 7 is displayed using print used for function or module 8 function’’’ documentation, explaining 9 x = 4 complex algorithms, or providing 10 y = 5 11 print(x+y) Output detailed insights into code functionality. 9 January 2024 PYTHON Key stage IV Class IX Demo 2 - Multi-line Comments Code 1 ’’’The code below initialises variable name and 2 age with the string value Sonam and integer 3 value 5 respectively. In the following code, 4 the value stored in these variables are 5 displayed using print function’’’ 6 name = “Sonam” 7 age = 16 8 print(name) 9 print(age) Output Sonam 16 January 2024 PYTHON Key stage IV Class IX Activity 1 - Displaying Message Write a Python program to display the message “ Gross National Happiness” and include a single-line comment that explains the purpose of the program. Code 1 #Printing Gross National Happiness 2 print("Gross National Happiness") Output Gross National Happiness January 2024 PYTHON Key stage IV Class IX Activity 2 - Writing Single and Multi-line Comments Write a Python Code program to 1 ’’’Program to welcome students with their name welcome a 2 and class to the world of coding.’’’ 3 name = "Pema Khandu" #Store a person's name student to coding. 4 Class = 9 #Store their grade Include 5 print("Welcome to Python Programming!")#Greeting comments, both 6 print("Name:",name) #Printing name single-line and 7 print("Class:",Class) # Printing grade multiline to provide clear Output explanations of the program. Welcome to Python Programming! Name:Pema Khandu Class:9 January 2024 PYTHON Key stage IV Class IX ACTIVITIES 1. Understanding Input Function 2. Demo 1 - Entering Favourite Subject and Ambition INPUT FUNCTION 3. Activity 1 - Requesting for a Name 4. Activity 2 - Displaying Subject Marks January 2024 PYTHON Key stage IV Class IX Input Function input() function is used to get information from the user while the program is running. You can ask the user for different types of information, like their name or age. Whatever the user types is stored as text (string data type), even if it looks like a number or some other data. input() function works by displaying a message to the user, then waiting for them to type something and press Enter. January 2024 PYTHON Key stage IV Class IX Input Function Example Output 1 name = input(“Enter your Name:”) Enter your Name: Sonam 2 print(f’My name is {name}’) My name is Sonam Example 1 num1 = int(input("Enter 1st No: ")) 2 num2 = int(input("Enter 2nd No: ")) 3 total = num1 + num2 Output 4 print(f"The Total is {total}”) Enter 1st No:34 Enter 2nd No:3 The Total is:37 January 2024 PYTHON Key stage IV Class IX Importance of input() function 1 2 3 4 Enable user input for Helps to gather various purposes, Support basic user information including data Facilitate the creation authentication, data interactively from the collection, user of interactive and validation, and user during program responses, and dynamic Python dynamic behavior execution. program programs. based on user input. customization. January 2024 PYTHON Key stage IV Class IX Demo 1 - Entering Favourite Subject and Ambition Write a Python program that asks the user to input their preferred subject and career ambition, stores the information in variables, and then prints the entered values. Code 1 fav_subject = input(“enter your favourite subject:”) 2 ambition = input(“enter your ambition:”) 3 print(“Your favorite subject is : ”,fav_subject) 4 print(“Your career ambition is :”,ambition) Output Your favorite subject is ICT Your career ambition is Data Analyst January 2024 PYTHON Key stage IV Class IX Activity 1 - Requesting For a Name A Python program asks the users to enter their name and then displays the name on the screen. a. Write the solution in your notebook for the above program. b. Test the codes in the Python IDLE. c. Make changes to the code if there are errors(debugging). Code 1 name = input('Please enter your name:') 2 #let us assume that you have entered “Dechen” 3 print(“Your name is :”,name) Output Please enter your name: Dechen Dechen January 2024 PYTHON Key stage IV Class IX Activity 2 - Displaying Subject Marks Write a Python program to ask the user to enter the marks of following subjects and display it accordingly: English Dzongkha Maths Science HCG Economics January 2024 PYTHON Key stage IV Class IX Activity 2 - Solution Code Output 1 eng = input('Enter English mark: ') Enter English mark: 89 Enter Dzongkha mark: 70 2 dzo = input('Enter Dzongkha mark: ') Enter Maths mark: 99 3 maths = input('Enter Maths mark: ') Enter Science mark: 89 3 sci = input('Enter Science mark: ') Enter HCG mark: 89 4 hcg = input('Enter HCG mark: ') Enter Economics mark: 90 5 eco = input('Enter Economics mark: ') Your Marks are as follows: 6 print('Your Marks are as follows: ') English: 89 Dzongkha:70 7 print(f“English: {eng}\n Maths:99 Dzongkha:{dzo}\n Maths: {maths}\n Science: 89 Science: {sci}\n HCG: {hcg}Economics: HCG: 89 {eco}”) Economics: 90 January 2024 PYTHON Key stage IV Class IX Key Points Comments are used to provide explanations to the code for better understanding. Single-line comments are for brief remarks on a single line that begins with # symbol. Multi-line comments are enclosed within triple quotes (""") and can span across multiple lines. Comments are not executed as part of the program; they are for human readers. Comments facilitate collaboration among programmers, allowing for easier understanding of code written by others. January 2024 PYTHON Key stage IV Class IX Key Points input() function is used to get information from the user while the program is running. In input() function, whatever the user types is stored as text (string data type), even if it looks like a number or some other data. input() function works by displaying a message to the user, then waiting for them to type something and press Enter. We use the input() function in programming to interact with the user and collect data or information while the program is running. January 2024 PYTHON Key stage IV Class IX དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག། Department of School Education Ministry of Education & Skills Development Python Coding Class IX ICT Curriculum January 2024 January 2024 PYTHON Key stage IV Class IX Key Concept # 8 GOOD PROGRAMMING PRACTICES January 2024 PYTHON Key stage IV Class IX Concept Overview Coding Concept Sub - Concepts Meaningful variable and function names Code readability Good Programming Debugging Practices ○ Types of errors (semantic and syntax error) Code optimisation January 2024 PYTHON Key stage IV Class IX Objectives Explain good programming practices with examples. Explain the importance of following good programming practices. Define debugging with examples. Differentiate between syntax errors and semantic errors. Identify the errors in programs. Correct the errors in programs. Apply code optimization technique for efficiency and performance of Python programs. January 2024 PYTHON Key stage IV Class IX Good Programming Practices Good programming refers to writing code that is efficient, easy maintainable, and follows best practices to enhance readability. Some of the the commonly used good programming practices are: Code testing and debugging Code optimization Code documentation January 2024 PYTHON Key stage IV Class IX Advantages of Good Programming Practices Good practices make your Python code easy to Easy to Understand read and follow. Well-organized code is easier to maintain and Less Hassle to Update update, saving time and effort. Writing modular code lets you reuse parts of it in Reuse Code different places, reducing repetition. Following best practices often leads to faster code Faster Performance execution. Clear Documentation Good practices include thorough documentation and Testing and testing for better reliability. January 2024 PYTHON Key stage IV Class IX ACTIVITIES 1 1. Understanding Debugging 2. Demo 1 - Identifying Code Errors DEBUGGING 3. Activity 1 - Finding Simple Interest January 2024 PYTHON Key stage IV Class IX Debugging Debugging in Python means finding and fixing mistakes, problems, or things that don't work as planned in a Python program. It's like being a detective and figuring out why the code isn't doing what it's supposed to do, and then making changes to make it work correctly. January 2024 PYTHON Key stage IV Class IX Importance of Debugging Debugging is essential for locating errors, Identifying Errors allowing developers to update issues. Debugging contributes to creating more Improving Code Quality reliable and maintainable software. Saving Time and Detecting issues early on helps in reducing Resources complex problems later. Optimizing Code Debugging enhances efficiency of programs Performance by reducing repetitive instructions. January 2024 PYTHON Key stage IV Class IX Demo 1 - Debugging Debug the code given below and Solution rewrite the correct code. Check the output. 1 Number_1 = 90 2 Number_2 = 3 Code 3 Total = Number_1 + Number_2 4 print(“The total of two 1 Number 1 = 90 numbers are:”,Total) 2 Number 2 = 3 3 Total = num1 + num2 4 print(“The total of two Output numbers are:,sum) The total of two numbers are: 93 January 2024 PYTHON Key stage IV Class IX Activity 1 - Finding Simple Interest There are bugs in the following code to find the simple interest. Study the code carefully and identify the errors. Then rewrite the correct codes and test the program. Make sure to write your codes in notebook before testing it in your computer. Code 1 Principle = int(input(“Enter the principal amount:)) 2 Year = int(input(“Enter the year:)) 3 Rate = input(“Enter rate:) 4 Simple Interest = principal*year+rate 5 print(“Simple Interest:”,Simple Interest) January 2024 PYTHON Key stage IV Class IX Activity 1 - Solution Code 1 principal=int(input(“Enter the principal:”)) 2 year=int(input(“Enter the year:”)) 3 rate=int(input(“Enter rate:”)) 4 Simple_Interest = principal*year*rate 5 print(“Simple Interest:”,Simple_Interest) Output Enter the principal:100 Enter the year:3 Enter rate:.08 Simple Interest:24 January 2024 PYTHON Key stage IV Class IX Errors In Python programming, an error refers to any issue or mistake that prevents the code from running as intended. Types of Error Syntax Errors Semantic Errors x==”PEMA” Div=x/0 January 2024 PYTHON Key stage IV Class IX Syntax and Semantic Errors Syntax Errors Semantic Errors A syntax error in programming A semantic error happens when occurs when the code violates there is mistake in the logic or the syntax rules of the understanding of the program's programming language. requirements. Syntax errors often result from Semantic errors produces misspelled keywords, missing outcomes that differ from what or misplaced punctuation, the programmer intended. incorrect indentation, or improper use of language These errors may involve constructs. incorrect variable assignments, flawed algorithms, or misuse of January 2024 functions. PYTHON Key stage IV Class IX Demo - Identifying Code Errors Observe the two given codes and identify the types of error. 2 1 1 x=0 1 x == 5: 2 y=2 2 print("x is:",x) 3 div=y/x 4 print(div) Syntax Error A syntax error arises when using = for Semantic Error comparison instead of ==, as == is the Since we cannot divide a number by 0, proper comparison operator, while = is it will cause an semantic error used for assignment January 2024 PYTHON Key stage IV Class IX Activity 1 - Identifying Types of Errors Review and identify the types of error in the codes provided below. Code 1 Code 2 Code 3 1 x=67 1 x=y=5 1 x=9 2 y=67 2 sum==x+y 2 y=”5” 3 sum=x+y+c 3 print(sum) 3 div=x/y 4 print(total) 4 print(div) Syntax Error Syntax Error Semantic Error Variable c is not Wrong Assignment of Dividing by String defined sum==x+y data is logical error January 2024 PYTHON Key stage IV Class IX ACTIVITIES 2 1. Understanding Code Optimization 2. Demo - Optimizing Code CODE OPTIMIZATION 3. Activity 1 - Finding Product of Three Numbers January 2024 PYTHON Key stage IV Class IX Code Optimization Code optimization refers to the process of improving the performance of a Python program by reducing its execution time and memory usage. Code optimization uses various techniques such as using more efficient algorithms, and utilizing language-specific features to enhance the performance. Example x,y,z=”karma”,24,”Khaling”# multiple assignments used print(“my name:”,x,”Age:”,y,” I am from:”,z) January 2024 PYTHON Key stage IV Class IX Demo - Optimizing Code Study the given program on addition of three numbers and rewrite the program by applying code optimization. Optimized Code Code 1 x=y=z=5 1 x=5 2 print(“Total:”,x+y+z) 2 y=5 3 z=5 Output 4 sum=x+y+z 5 print(“Total”,sum) Total:15 January 2024 PYTHON Key stage IV Class IX Activity 1 - Finding Product of Three Numbers Study the given program on multiplying three numbers and rewrite the program by applying code optimization. Code Solution 1 x=5 1 x=y=5 2 y=5 2 z=6 3 z=6 3 print(“Product:”,x*y*z) 4 Product=x*y*z 5 print(“Product”,Product) Output Product:150 January 2024 PYTHON Key stage IV Class IX ACTIVITIES 3 1. Understanding Code Optimization 2. Demo - Finding Total of CODE DOCUMENTATION Three Numbers 3. Activity 1 - Finding Product of Three Numbers January 2024 PYTHON Key stage IV Class IX Code Documentation Code documentation is the process of providing explanations and instructions within the source code to help other developers understand its purpose, functionality, and usage. Some of the the commonly used code documentation are: Inline comments within the code provide explanations Comments for specific lines and clarify complex logic. Function For each function or method, document its purpose, Documentation parameters, return values, and any exceptions it may raise. Include examples of how to use functions or classes Usage Examples to demonstrate their intended usage. January 2024 PYTHON Key stage IV Class IX Example Write a Python program to find the addition of any two numbers when numbers are entered by the user. Code 1 ’’’ 2 Program is find the addition of any two numbers. -Read any two numbers as an input. 3 -Compute two numbers entered by the users. 4 -Display Result. 5 ’’’ 6 number1 = float(input(“Enter Number1:”)) 7 number2 = float(input(“Enter Number2:”)) 8 print(f’Total:{number1+number2}’) January 2024 PYTHON Key stage IV Class IX Example January 2024 PYTHON Key stage IV Class IX Key Points Good programming" refers to writing code that is efficient, easy to maintain, and follows best practices to enhance readability. Some of the the commonly used good programming practices are: Advantages of Good Programming Practices Debugging is a process of finding errors or bugs within the code to resolve the issues leading to incorrect output. There are two types of errors in Python - syntax error and semantic error. Code optimization involves identifying and eliminating repetitive code to make the program more readable and manageable. Code documentation… January 2024 PYTHON Key stage IV Class IX དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག། Department of School Education Ministry of Education & Skills Development Python Coding Class IX ICT Curriculum January 2024 January 2024 PYTHON Key stage IV Class IX Key Concept # 9 DATA TYPES January 2024 PYTHON Key stage IV Class IX Concept Overview Coding Concept Sub - Concepts Definition of data type Importance of data types in programming Classification of data types Data Types Type casting Escape characters January 2024 PYTHON Key stage IV Class IX Objectives Define data types with examples. Explain the importance of data types in programming. List different categories of data types in Python. Differentiate between the various data types. Interchange data types using type casting functions in Python programs. Apply commonly used escape characters to display the desired output. January 2024 PYTHON Key stage IV Class IX ACTIVITIES 1. Understanding Data Types 2. Demo 1 - Accessing Characters Using Slice Operator 3. Activity 1 - Accessing Substring 4. Activity 2 -Converting the String 5. Demo 2 - Finding Average 6. Demo 3 - Displaying Message DATA TYPES 7. Activity 3 - Calculating Hypotenuse 8. Demo 4 - Converting integer to float 9. Activity 4 - Converting float to integer 10. Demo 5 - Adding Two Numbers 11. Activity 5 - Adding integer and float Values 12. Activity 6 - Converting integer to string Data 13. Activity 7 - Escape Characters January 2024 PYTHON Key stage IV Class IX Data Types Data types represent the types of data present inside a variable. In other words, a data type is like a label that tells the computer what kind of information is stored in a variable. A data type could be a number, a piece of text, or a true/false value. Example: “Pema” is a string data stored in variable “Name” Name = "Pema" Variable Value January 2024 PYTHON Key stage IV Class IX The Importance of Data Types in Programming Data types ensure that the right amount of 1. Efficient Resource memory is allocated for variables, preventing unnecessary consumption. Data types contribute to the early detection 2. Error Prevention of errors, improving the reliability. 3. Code Readability Defining data type enhances code readability. January 2024 PYTHON Key stage IV Class IX Classifications of Data Types In Python, data are classified into various types. The following diagram shows the classification of data types in Python. Classification of Python Data Types 1. Numeric 2. Text 3. Sequence 4. Mapping 5. Set 6. Boolean Types Type Types Type Types Type int list set float str tuple dict bool frozenset complex range January 2024 PYTHON Key stage IV Class IX Python Data Types - Numeric Types 1. Numeric Types a) int b) float c) complex Represents whole Represents complex Represents floating- numbers (integral numbers in the form of point numbers values), such as 5, -10, a + bj, where a and b (decimal numbers), or 0. are real numbers and j such as 3.14, -2.5, or 0.0. is the imaginary unit. ICT_ marks=78 y = -4.5 c = 4+8j Note: Floats can be expressed in scientific notation and we can represent big values with less memory [example: 980000000 can be represented as 9.8e8]. January 2024 PYTHON Key stage IV Class IX Python Data Types (cont.) 2. Text Type A string is a sequence of characters within a quotation mark.It is written as str. String data types are usually enclosed within a single (‘ ’), double (“ ”) or triple (‘‘‘ ’’’) quotes. Example: school_name = ‘Zilukha MSS’ school_name = “Zilukha MSS” school_name = ‘‘‘Zilukha MSS’’’ print(school_name) January 2024 PYTHON Key stage IV Class IX String Data Type There are three types of quotes to represent a string in Python. 1. Single Quote(‘ ‘) 2. Double Quote(“ “) To use a double quote as a normal To use a single quote as a normal character, then enclosed by the character, then enclosed by the single quote. double quote. Code Code 1 msg = ‘ “Hello” ‘ 1 msg = “ ‘Hello’ “ 2 print(msg) 2 print(msg) Output Output 1 “Hello” 1 ‘Hello’ January 2024 PYTHON Key stage IV Class IX String Data Type (cont.) 3. Triple Quote(’ ’ ’ OR “ ” ”) Triple quote is used to: define multi-line strings. display text that are enclosed within single or double quotation marks. define documentation string(doc_string). Code Output 1 x = ””” ‘Embrace the challenges, 2 ‘Embrace the challenges, for every problem solved, 3 for every problem solved, is a step towards mastery.’ 4 is a step towards mastery.’ 5 ””” 6 print(x) January 2024 PYTHON Key stage IV Class IX Accessing Characters of a String There are two ways to access characters from a string. 1 By using indexing 2 By using slice operator January 2024 PYTHON Key stage IV Class IX 1. Accessing Characters Using Indexing Index: refers to the position or location of an element within a defined range of strings. There are two ways of indexing either +ve or -ve. ○ +ve index means forward direction from Left to Right [it follows zero-based index]. ○ -ve index means backward direction from Right to Left. January 2024 PYTHON Key stage IV Class IX Example animal = “cat” +ve index 0 1 2 animal c a t -ve index -3 -2 -1 +ve indexing -ve indexing ○ animal #c ○ animal[-1] #t ○ animal #a ○ animal[-2] #a ○ animal #t ○ animal[-3] #c January 2024 PYTHON Key stage IV Class IX 2. Accessing Characters Using Slice Operator Slice operator is used to find the substring from the string. [ ] operator is called slice operator, which can be used to retrieve parts of string. Syntax: variable_name[start : stop: step] Start : It indicates the Index where slice has to Start. Default Value is 0. Stop : It indicates the Index where slice has to End. Step : increment value. Default Value is 1. January 2024 PYTHON Key stage IV Class IX Example country = “BHUTAN” +ve 0 1 2 3 4 5 country B H U T A N -ve -6 -5 -4 -3 -2 -1 +ve Indexing -ve Indexing ○ print(country[1:3]) ○ country[-5:-3] #HU #HU ○ country[-6:-3] #BHU ○ country[:3] #BHU ○ country[-4:] #UTAN ○ country[2:] #UTAN ○ country[-6:-1:2] #BUA ○ country[0:5:2]#BUA ○ country[:] #BHUTAN January 2024 PYTHON Key stage IV Class IX Mathematical Operators for String The commonly used operators to manage string data types are: 1. String Concatenation (+) 2. String Repetition ( * ) String concatenation is used to combine String repetition is used to repeat a or join more than one character or string. character or string for multiple times. Code Code 1 print(“I”+”Love”+”Bhutan”) 1 print(“#”*10) Output Output 1 I Love Bhutan 1 ########## January 2024 PYTHON Key stage IV Class IX Demo 1 - Accessing Characters using Slice Operator Write a Python program to find the substring from the following strings using slice operator. 1. Display “yangtse” from the string “Tashiyangtse”. 2. Display “pema” from the string “tpaesmhai”. Code Output 1 address = “Tashiyangtse” 1 yangtse 2 print(address[5:12]) Code Output 1 Mix_letter = “tpaesmhai” 1 pema 2 print(Mix_letter[1:8:2]) January 2024 PYTHON Key stage IV Class IX Activity 1 - Accessing Substring Study the code given and answer the questions that follow: Name = “Sangay Dorji” Write a program that prints the sub-string “joy” and “Dorji”. Code Output 1 Name = “Sangay Dorji” 1 joy 2 print(Name[-2]+Name[-4]+Name) 2 Dorji 3 print(Name[7:]) January 2024 PYTHON Key stage IV Class IX String Methods The following are some of the string methods used in Python. SL. No Method Description syntax Convert the first character of a x=”karma” 1 capitalize() string to uppercase. print(x.capitalize())#Karma x=”sonam dorji” Converts the first character of each 2 title() print(x.title())#Sonam word to uppercase. Dorji Remove spaces at the beginning x=” sonam “ 3 strip() and at the end of the string: print(x.strip())#sonam x=”SONAM” 4 lower() Convert a string to lowercase. print(x.lower())#sonam x=” sonam” 5 upper() Convert a string to uppercase. print(x.upper())#SONAM January 2024 PYTHON Key stage IV Class IX Activity 2 - Converting the String Write a Python program to convert following strings from. 1. “Tsaling Ps” to “TSALING PS” 2. “mendrelgang school” to “Mendrelgang School” 3. “Tshewang Choden” to “tshewang choden” Code Output 1 address=”Tsaling Ps” 1 TSALING PS 2 school=” mendrelgang school” 2 Mendrelgang School 3 name=”Tshewang Choden” 3 tshewang choden 4 print(address.upper()) 5 print(school.title()) 6 print(name.lower()) January 2024 PYTHON Key stage IV Class IX Python Data Types(cont.) 3. Sequence Types a) List b) Tuple c) Range is a collection which is is a collection which is returns a sequence of ordered, changeable, ordered, unchangeable, numbers. and enclosed in square and enclosed in It is commonly used in brackets [ ]. parentheses for loops to generate a brackets ( ). series of numbers. X = [2, 4, 5, 6] X = (2, 4, 5, 6) range(5) January 2024 PYTHON Key stage IV Class IX Python Data Types(cont.) 4. Mapping Type 5. Set Types Dictionaries a) set( ) b) frozenset( ) is an unordered Similar to sets, they are are used to store data collection and enclosed immutable and cannot values in key:value in curly brace { }. be changed once pairs. defined as the frozen set. dict = {“name” : ”Pema”} Number ={3, 4, 5} X = frozenset({2, 4, 5}) key value January 2024 PYTHON Key stage IV Class IX Python Data Types(cont.) 6. Boolean Type Represents the truth value, either true or false. It is often used for logical operations and conditions. Example 1 X = 5 2 Y = 10 3 if x>y: Output 4 print(True) 5 else: 1 False 6 print(False) January 2024 PYTHON Key stage IV Class IX Demo 2 - Finding Average Age a. Assign different ages to three Code students (use integer data type). 1 age_1=12 b. Calculate the average age of the 2 age_2=13 three students and display the average. 3 age_3=15 4 total=age_1+age_2+age_3 Output 5 avg=total/3 Average:13.3 6 print(“Average:”,avg) January 2024 PYTHON Key stage IV Class IX Demo 3 - Displaying Message A. Assign names to the variables Code (use string data type). 1 std_1=”Chandra” B. Concatenate the names to create 2 std_2=”Karma” a message welcoming them to the 3 message=”Welcome to the class. class”+std_1+”and”+std_2 C. Print the concatenated message. 4 print(message) Output Welcome to the class Chandra and Karma January 2024 PYTHON Key stage IV Class IX Activity 3 - Calculating Hypotenuse Write a Python program to Code - Solution calculate the hypotenuse of a right-angled triangle with the 1 import math base 20 and the opposite side is 2 base=20 30. 3 opp=30 hypo=math.sqrt( base**2 + 4 opp**2 ) print(“Hypotenuse of a Output Right_angle Triangle”,hypo) Hypotenuse of a Right_angle Triangle 36.06 January 2024 PYTHON Key stage IV Class IX Type Casting Type casting in Python refers to the process of converting a variable from one data type to another. Built-in functions in Python for type casting 1. int( ) 2. float( ) 3. complex( ) 4. str( ) 5. bool( ) January 2024 PYTHON Key stage IV Class IX Type Casting Argument Arguments int float complex bool str /functions Eg: int(“30”) int() Eg: float(“40”) float() function Eg: complex(“30) complex() Eg: complex(10,20) bool() str() January 2024 PYTHON Key stage IV Class IX Demo 4 - Converting int to float Data Type Write a Python program to convert int data type to float data type. Code 1 x = 10 2 print(float(x)) Output 10.0 January 2024 PYTHON Key stage IV Class IX Activity 4 - Converting float to int data type Write a Python program to convert float data type to int data type. Code 1 x = float(input(“Enter a float number:”)) 2 print(int(x)) Output Enter a float number: 20.5 20 January 2024 PYTHON Key stage IV Class IX Demo 5 - Adding Two Number Write a Python program to find the sum of two numbers entered by user.. Code 1 x = float(input(“Enter First Number:”)) 2 y = float(input(“Enter Second Number:”)) 3 Total = x + y 4 print(“The Total of Two Number:”,Total) Output Enter First Number: 5 Enter Second Number: 10 The Total of Two Number: 15.0 January 2024 PYTHON Key stage IV Class IX Activity 5 - Adding integer and Floating Values Write a Python program to create the variable x and assign it a string value of ‘3.4’. Create another variable y and set it to 20. Add the two numbers and display the result. Code 1 x = ‘3.4’ 2 y = 20 Output 3 z = float(x) Total:23.4 4 print(“Total:”, y+z) January 2024 PYTHON Key stage IV Class IX Activity 6 - Converting Integer to String Data Write a Python program to create a variable “subject” and assign it a string value of ‘Dzongkha’. Create another variable mark and set it to 75. Combine these two variables by converting the mark to a string and print the result. Code 1 subject = “Dzongkha” 2 mark = 75 Output 3 print(subject,”:”,str(mark)) Dzongkha : 75 January 2024 PYTHON Key stage IV Class IX Escape characters Escape characters are special characters that are used to represent characters that are difficult to type directly into a string. Here are some commonly used escape characters in Python: \n : Represents a newline character that insert a line break. \t : Represents a tab character. \” : Represents a double quotation mark character. \' : Represents a single quotation mark character. \\ : Represents a backslash character. It is used to insert a “\” within a string. January 2024 PYTHON Key stage IV Class IX Activity 7 - Use of Escape Characters Use one print() function and escape characters to display the following output. Output Code Sonam 1 print(“Sonam\nJigme”) Jigme Jigme Dorji 2 print(“Jigme\tDorji”) I love “Python” 3 print(“I love \”Python\””) He is Sonam’s father 4 print(“He is Sonam\’s father”) He likes Tea\Coffee 5 print(“He likes Tea\\Coffee”) January 2024 PYTHON Key stage IV Class IX Key Points Data types define the properties and features of values that can be stored in the variable. Data types helps in preventing error and enhancing code readability. Different types of data type used in Python are numeric, text, boolean, sequence, mapping and set. We can use indexing and slice operator to access different characters from a string. Type casting involves converting data from one type to another. Escape characters are used to display output in the desired format. January 2024 PYTHON Key stage IV Class IX དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག། Department of School Education Ministry of Education & Skills Development Python Coding Class IX ICT Curriculum January 2024 January 2024 PYTHON Key stage IV Class IX Key Concept # 9 OPERATORS January 2024 PYTHON