This course module is a comprehensive primer on Python programming, diving deep into types, integers, floating-point numbers, and strings for initialization. Further, mathematical operations will be expressed using expressions, and variable value storage will be understood by the learner.
The module will dive deeper into string manipulation to ensure strong capability in text data work and processing. By the end of the module, the learner will be grounded in Python, ready to take on more complex programming concepts.
Learning Outcomes
Illustrate Python data types by showing understanding of type conversion or casting between types like string, float, and integer.
Interpret variables and resolve expressions through application of mathematical operations.
To describe various methods and operations, to transform or manipulate strings.
Build a program in JupyterLab to demonstrate the knowledge within the data types, expressions, and variables at play.
Work with, manipulate, and perform operations on strings in Python.
PRACTICE QUIZ
1. What is the type of the following: 0
float
int (CORRECT)
So there is no decimal, therefore the number is int in format. You can also use type function in Python to check the datatype of any value.
2. What is the type of the following number: 3.12323
float (CORRECT)
int
Yes, definitely, since it has a decimal point, it is actually a float type. You can confirm this by using type() function in Python as given below.
3. What is the result of the following: int(3.99)
3 (CORRECT)
3.99
Correct. In Python, converting a float into an integer truncates anything after the decimal point. So, converting float 3.99 into an integer produces integer 3, deleting the digits .99.
PRACTICE QUIZ
1. What is the result of the following operation: 11//2
5.5
5 (CORRECT)
This is a nature of operation that always rounds off downwards to negative infinity, while casting as an integer has the effect of truncating or rounding off towards zero.
2. what is the value of x after the following is run:
x=4
x=x/2
2.0 (CORRECT)
4.0
Correct: Yes, that’s right! The statement x = x / 2 will change x to the result of x divided by 2. Since python’s division always returns a float (even for integers) the result will be a float. For
PRACTICE QUIZ
1. What is the result of the following: Name[0]
“M” (CORRECT)
“i”
“n”
Indeed, indexing starts at 0 in Python. This means that the first item in a list, tuple, or string is accessed by reference to index 0.
2. What is the result of the following: Name[-1]
“M” (CORRECT)
“i”
“n”
correct, The index 0 corresponds to the first index
3. What is the output of the following: print(“AB\nC\nDE”)
ABC
DE
AB
CD
E
AB
C
DE (CORRECT)
That is right! In Python, \n is an escape character that represents a new line in the program. In other words, when the print function encounters \n in a string, it moves the output to the next line.
4. What is the result of following?
“hello Mike”.find(“Mike”)
If you are unsure, copy and paste the code into Jupyter Notebook and check.
6,7,8
6,7,8
5
6 (CORRECT)
correct, the method finds the starting index of a substring
MODULE 1 GRADED QUIZ
1. What is the value of x after the following lines of code?
x=2
x=x+2
4 (CORRECT)
2
Correct: This x = x + 2 is appropriately doing that; adding 2 to the existing value of x and updating the value of x. In truth, it is a shorthand for taking the current value, adding 2 to it, and putting the result in x again.
2. What is the result of the following operation 3+2*2 ?
3
7 (CORRECT)
9
Correct, Python follows the standard mathematical conventions
3. What is the result of the following code segment: type(int(12.3))
str
float
int (CORRECT)
correct, Truly! In the code, you can cast or convert a float to an integer. Then, call the type() function to check the type of the resulting value.
4. What is the result of the following code segment: int(True)
1 (CORRECT)
0
error
correct, when you cast a boolean True to an integer you get a 1
5. In Python, what is the result of the following operation: ‘1’+’2′ ?
3
‘3’
’12’ (CORRECT)
correct, Absolutely! The + operator in_python is used to note the strings and NOT their addendum in the mathematic sense. The application of this operator on strings joins their carrier to work on it as a new single string.
6. Given myvar = ‘hello’ , how would you return myvar as uppercase?
len(myvar)
myvar.find(‘hello’)
myvar.upper() (CORRECT)
7. What is the result of the following : str(1)+str(1) ?
’11’ (CORRECT)
2
correct, Correct! Here, the operator ‘+’ for concatenation does not refer to its mathematical property of addition but is used to join the strings in Python. Applying the operator ‘+’ to two string objects gives rise to a single string object that results from their being joined.
8. What is the result of the following: “ABC”.replace(“AB”, “ab”) ?
‘abC’ (CORRECT)
‘ABc’
correct, Exactly! When trying to add something using the + operator which is integers and strings, it first needs to convert or cast the integers to strings so they can be joined. It is a failed attempt for integers with strings which have not yet been converted. A TypeError in Python occurs.
9. In Python 3, what is the type of the variable x after the following: x=1/1 ?
float (CORRECT)
int
correct, in Python 3, regular division always results in a float
10. What is the value of x after the following lines of code?
x=1
x=x+1
1
2 (CORRECT)
4
Correct: Right. That means in Python the replace() method gives a new string where all occurrences of a specified substring are replaced by another substring. The original string is untouched, because strings are immutable in Python; instead, it will produce and return a changed copy of the original string.
11. What is the result of the following code segment: int(False)
1
0 (CORRECT)
error
correct, when you cast a boolean False to an integer you get a 0
12. What is the result of the following: ‘hello’.upper() ?
‘HELLO’ (CORRECT)
‘Hello’
‘hello’
correct, That’s exactly what it is! In Python, the upper() method returns a new string that converts all the alphabetic characters to upper case; does not modify the original since the strings in Python are immutable.
13. What is the result of the following operation 1+3*2 ?
7 (CORRECT)
12
8
Sure, Python extensively follows standard mathematically agreed conventions for all its operators and expressions. This pertains to arithmetic operations, order of operations (or precedence), and certain functions.INTERNAL_ERROR
14. What is the result of the following : str(1+1) ?
‘2’ (CORRECT)
’11’
Quite right! It goes like the evaluation of an argument first in Python and then processing it such as in expression 1 + 1, where 1 + 1 becomes 2, and if one wishes to convert it to string for printing and other purposes, str() can be called to convert the integer 2 into a string “2”.
15. What is the result of the following: “123”.replace(“12”, “ab”) ?
‘ab3’ (CORRECT)
‘123ab’
correct, That’s right! The Python replace() method returns a new string which has all occurrences of a specified substring replaced with another substring. This method does not change the original string, as strings are immutable in Python. Rather, it creates and returns a modified copy.
16. What is the type of the following “7.1”
float
string (CORRECT)
correct, the type is string
17. In Python 3, what is the type of the variable x after the following: x=2/2 ?
float (CORRECT)
int
correct, in Python 3, regular division always results in a float
CONCLUSION – Python Basics
It looks like you’re working on a Python programming module focused on foundational skills! This summary outlines key learning objectives. Would you like to dive deeper into any of these areas, such as string manipulation techniques, mathematical operations, or advanced topics to explore next? Let me know how I can assist you further.