🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

CheerfulUvarovite

Uploaded by CheerfulUvarovite

University Canada West

Tags

python programming coding exercises computer science

Full Transcript

Python - Introduction keyboard_arrow_down Display Specified Message in the Screen [] print("Hello World!") Hello World! Notes: 1. print is a python syntax. python is a case-sensitive language, means that the syntaxes should all in lower case. 2. Hello World! is a string we w...

Python - Introduction keyboard_arrow_down Display Specified Message in the Screen [] print("Hello World!") Hello World! Notes: 1. print is a python syntax. python is a case-sensitive language, means that the syntaxes should all in lower case. 2. Hello World! is a string we want to display. It is not syntax, so it can be in upper case, lower case or a combination of both. 3. strings are surrounded with single/double quotes. keyboard_arrow_down Creating a Comment Comments starts with a #, and Python will ignore them: [] #This is a comment print("Hello, World!") print("Hello, World!") #This is a comment Hello, World! Hello, World! Exercise 1: Write a Python program that prints your name. Add a comment to it. [] # YOUR CODE HERE # keyboard_arrow_down Variables Variables are used to temporarily store attributes with their values. Let's declare some variables: [] k = 10 number = 10.3 Number = -2 num_ = 100 my_name = 'Mohsen' text = "Hello World!" We can use print to display the value stored in a variable: [] print(text) Hello World! Exercise 2: Declare a variable to store your email address and then print it. [] # YOUR CODE HERE # keyboard_arrow_down Strings [] text = "Hello World!" [] text.upper() [] text.lower() [] text.find('o') 4 keyboard_arrow_down Arithmetic operations [] a = 10 b = 18 c=a+b print(c) 28 Exercise 3: Declare two variables for length and width of a rectangle.  length = 3  width = 6 Calculate the area of the rectangle (length × width) and declare it as a new variable. Print the value of area. [] a = 10 b = 18 c=a+b c1 = a - b c2 = a * b c3 = a / b c4 = a ** 0.5 print(c, c1, c2, c3, c4, sep='\n') 28 -8 180 0.5555555555555556 3.1622776601683795 [] # YOUR CODE HERE # length = 3, width = 6 area = length * width print("The area of a rectangle with the width of", width, "and the length of", length, "is", area) # f-tring The area of a rectangle with the width of 6 and the length of (3,) is (3, 3, 3, 3, 3, 3) [] length = 3, width = 6 area = length * width print(f"The area of a rectangle with the width of {width} and the length of {length} is {area}") # f-tring The area of a rectangle with the width of 6 and the length of (3,) is (3, 3, 3, 3, 3, 3) keyboard_arrow_down User Input We can user input with the input() function. [] name = input("Please enter your name:") Please enter your name:Mia [] print("your name is:", name) your name is: Mia [] salary = int(input("Enter your age: ")) print("your salary is:", salary) Double-click (or enter) to edit casting is convert Exercise 4: Write a program that asks the user for their age and then prints their year of birth. [] # YOUR CODE HERE # Year_of_birth_str = input("Enter your age: ") Year_of_birth_int = int(Year_of_birth_str) print("your year of birth is:", Year_of_birth_int) Enter your age: 38 your year of birth is: 38 [] current_year = 2024 age = int(input("Enter your age: ")) year_of_birth = current_year - age print("your year of birth is:", year_of_birth) Enter your age: 38 your year of birth is: 1986 [] current_year = 2024 age = int(input("your age?")) year_of_birth = current_year - age print(f"your year of birth is: {year_of_birth}") your age?39 your year of birth is: 1985 keyboard_arrow_down cach hoi chatgpt: use ipywidget I am running the code in colab of google... keyboard_arrow_down Conditional Statements [] age = int(input("Enter your age: ")) if age < 18: print("You are a minor.") else: print("You are an adult.") [] age = 18 if age < 18: print("You are a minor.") # watch the indent print(1) print(2) elif age == 18: # "=" is value, "==" is comparative sentence print("You are exactly 18.") else: print("You are an adult.") print("bye") You are exactly 18. bye Exercise 5: Write a program that checks if a given number is positive, negative, or zero. [] # YOUR CODE HERE # keyboard_arrow_down Python Loops [] for i in range(5): # collect from 0 to 10 print("Iteration", i) Using for loop to calculate sum of all numbers from 1 to 10. [] s=0 for i in range(11): s=s+i print(s) 55 keyboard_arrow_down Python Lists Lists are used to store multiple items in a single variable. [] fruit_list = ["apple", "banana", "cherry"] print(fruit_list) ['apple', 'banana', 'cherry'] [] print(fruit_list) cherry keyboard_arrow_down Range of Indexes You can specify a range of indexes by specifying where to start and where to end the range. When specifying a range, the return value will be a new list with the specified items. [] print(fruit_list[1:3]) ['banana', 'cherry', 'pearl', 'melon']  Changeable: The list is changeable, meaning that we can change, add, and remove items in a list after it has been created. [] # change fruit_list = ["apple", "banana", "cherry"] fruit_list = "blackberry" print(fruit_list) ['blackberry', 'banana', 'cherry'] [] # add fruit_list.append("tomato") print(fruit_list) ['blackcurrant', 'banana', 'cherry', 'tomato'] [] # insert fruit_list.insert(1, "orange") print(fruit_list) ['blackcurrant', 'orange', 'banana', 'cherry', 'tomato'] [] # join two lists list1 = ["apple", "banana", "cherry"] list2 = ["apple", "pearl", "kiwi"] print(list1+list2) ['apple', 'banana', 'cherry', 'apple', 'pearl', 'kiwi'] [] # multiply list by a number fruits = ["apple", "banana", "cherry"] mytuple = fruits * 2 print(mytuple) ['apple', 'banana', 'cherry', 'apple', 'banana', 'cherry'] [] # remove fruit_list.remove("tomato") print(fruit_list) ['blackcurrant', 'orange', 'banana', 'cherry'] [] # [1, "Hi", "aple"] keyboard_arrow_down Length of list [] len(fruit_list) 5 keyboard_arrow_down Sort List [] fruit_list = ["orange", "mango", "kiwi", "pineapple", "banana"] fruit_list.sort() print(fruit_list) ['banana', 'kiwi', 'mango', 'orange', 'pineapple'] [] fruit_list = ["orange", "mango", "kiwi", "pineapple", "banana"] fruit_list.sort(reverse = True) print(fruit_list) ['pineapple', 'orange', 'mango', 'kiwi', 'banana'] Exercise 6: Write a program that creates a list of these numbers: 100, 50, 65, 82, 23. Then sort the list and print the result. [] # YOUR CODE HERE # keyboard_arrow_down Loop through a list [] fruit_list = ["apple", "banana", "cherry"] for x in fruit_list: print(x) apple banana cherry [] final_list = [] for i in [1, 2, 3]: final_list.append(i*2) final_list keyboard_arrow_down Functions A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result. Creating a Function In Python a function is defined using the def keyword: [] def my_function(): print("Hello!") Calling a Function To call a function, use the function name followed by parenthesis: [] my_function() Hello! keyboard_arrow_down Arguments Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma. [] def hi_function(name): print("Hi", name, "How are you?") hi_function("Mohsen") Hi Mohsen How are you? [] def add_num(x, y=1): print(x+y) add_num(10,-3) add_num(10) 7 11 [] def my_multi(list_, k): final_list = [] For i in list : final_list.append(i*k) return final_list my_multi([1, 2, 3], 5) final_list Exercise 7: Define a function that takes the height and weight and calculate the BMI: BMI=weight(kg)height(m)2

Use Quizgecko on...
Browser
Browser