Podcast
Questions and Answers
Which of the following best describes the role of documentation in programming?
Which of the following best describes the role of documentation in programming?
- It automatically translates the code into different programming languages.
- It prevents the code from being executed by unauthorized users.
- It makes the code run faster by optimizing the interpreter's operations.
- It is essential for explaining what the code does, why it does it, and how to use it. (correct)
Why is it important to adhere to a programming style such as PEP 8 when writing Python code?
Why is it important to adhere to a programming style such as PEP 8 when writing Python code?
- It reduces the number of lines of code required to implement a feature.
- It makes the code easier to read, debug, and maintain by ensuring a consistent and standardized format. (correct)
- It ensures that the code is compatible with older versions of Python.
- It allows the code to run without the need for an interpreter.
In Python, how are single-line comments typically denoted?
In Python, how are single-line comments typically denoted?
- Using triple quotes (`'''` or `"""`)
- Using a hash symbol (`#`) (correct)
- Using double slashes (`//`)
- Using angle brackets (`<!-- -->`)
Which of the following is true about multi-line comments (docstrings) in Python?
Which of the following is true about multi-line comments (docstrings) in Python?
Which of the following is NOT a valid guideline for writing self-explanatory and maintainable code?
Which of the following is NOT a valid guideline for writing self-explanatory and maintainable code?
What consideration should be taken when choosing variable names in Python?
What consideration should be taken when choosing variable names in Python?
In Python, which data type is used to represent whole numbers without any decimal places?
In Python, which data type is used to represent whole numbers without any decimal places?
What distinguishes a float
data type from an int
data type in Python?
What distinguishes a float
data type from an int
data type in Python?
Which data type would you use to store a value that represents whether a condition is true or false?
Which data type would you use to store a value that represents whether a condition is true or false?
How are string literals represented in Python?
How are string literals represented in Python?
What is the purpose of escape sequences in Python strings?
What is the purpose of escape sequences in Python strings?
Which escape sequence is used to insert a newline character into a Python string?
Which escape sequence is used to insert a newline character into a Python string?
What is the operation called when you combine two or more strings in Python?
What is the operation called when you combine two or more strings in Python?
Which operator is used for string concatenation in Python?
Which operator is used for string concatenation in Python?
What is a variable in Python?
What is a variable in Python?
What is the primary purpose of variable assignment in Python?
What is the primary purpose of variable assignment in Python?
Which of the following is NOT a valid rule for naming variables in Python?
Which of the following is NOT a valid rule for naming variables in Python?
Given the expressions A = 10
, B = 20
, and then C = A + B
, what will C
store?
Given the expressions A = 10
, B = 20
, and then C = A + B
, what will C
store?
Which of the following variable names is invalid in Python?
Which of the following variable names is invalid in Python?
What is an expression in Python?
What is an expression in Python?
What does an arithmetic expression consist of?
What does an arithmetic expression consist of?
In Python, what is the result of the expression 10 // 3
?
In Python, what is the result of the expression 10 // 3
?
If you divide one integer by another integer using the //
operator, what type of result will you obtain?
If you divide one integer by another integer using the //
operator, what type of result will you obtain?
What will be the data type of the variable result
after executing result = 5 / 2.0
?
What will be the data type of the variable result
after executing result = 5 / 2.0
?
Given the code x = 1 / 2.0
, what value will x
be assigned?
Given the code x = 1 / 2.0
, what value will x
be assigned?
What is the primary purpose of the type()
function in Python?
What is the primary purpose of the type()
function in Python?
Which function is used for explicit type conversion in Python?
Which function is used for explicit type conversion in Python?
If you have a float variable my_float = 3.143
, how would you convert it to an integer?
If you have a float variable my_float = 3.143
, how would you convert it to an integer?
What happens when you try to convert a string that contains letters (e.g., 'a'
) into an integer using int()
?
What happens when you try to convert a string that contains letters (e.g., 'a'
) into an integer using int()
?
What do logical expressions evaluate to in Python?
What do logical expressions evaluate to in Python?
Which logical operator returns True only if both conditions are True?
Which logical operator returns True only if both conditions are True?
What is the purpose of relational operators in Python?
What is the purpose of relational operators in Python?
In Python, what does the operator ==
do?
In Python, what does the operator ==
do?
Given the expression (5 > 3) and (10 < 5)
, what will it evaluate to?
Given the expression (5 > 3) and (10 < 5)
, what will it evaluate to?
Which of the following represents the correct order of operator precedence in Python (from highest to lowest)?
Which of the following represents the correct order of operator precedence in Python (from highest to lowest)?
Based on operator precedence, what is the result of the expression 5 + 2 * 3
?
Based on operator precedence, what is the result of the expression 5 + 2 * 3
?
What is the output of the following Python code?
print(type(5))
What is the output of the following Python code?
print(type(5))
Flashcards
Programming Style
Programming Style
The way you write code; good style makes code easier to read and maintain.
Documentation
Documentation
Explanatory remarks in code helping others understand its structure and functionality.
Comments in Python
Comments in Python
Used to explain code;ignored by the interpreter. Single-line with #, multi-line with triple quotes.
Escape Sequences
Escape Sequences
Signup and view all the flashcards
String Operations
String Operations
Signup and view all the flashcards
Variable Definition
Variable Definition
Signup and view all the flashcards
Variable Assignment
Variable Assignment
Signup and view all the flashcards
Variable Naming Rules
Variable Naming Rules
Signup and view all the flashcards
Expression Definition
Expression Definition
Signup and view all the flashcards
Arithmetic Expression
Arithmetic Expression
Signup and view all the flashcards
Integer Division (//)
Integer Division (//)
Signup and view all the flashcards
Float Division (/)
Float Division (/)
Signup and view all the flashcards
Type Function
Type Function
Signup and view all the flashcards
Explicit Type Conversion
Explicit Type Conversion
Signup and view all the flashcards
Logical Expression
Logical Expression
Signup and view all the flashcards
Relational Operators
Relational Operators
Signup and view all the flashcards
Operator Precedence
Operator Precedence
Signup and view all the flashcards
Data Types
Data Types
Signup and view all the flashcards
Integer (int)
Integer (int)
Signup and view all the flashcards
Float
Float
Signup and view all the flashcards
String (str)
String (str)
Signup and view all the flashcards
Boolean
Boolean
Signup and view all the flashcards
Study Notes
- CSC1024 is Programming Principles, Week 02
- The subject leader is Dr Danish Mahmood Khan, a Senior Lecturer at the Academic Staff Office (UW 3-2); his email is [email protected]
Assessment Dates / Marks Allocation
- Quiz 1 accounts for 25% of the grade and covers weeks 01-02
- Quiz 1 will occur during the lecture in Week 3, on the 26th of Feb 2025, and will consist of MCQs
- Quiz 2 accounts for 25% of the grade and covers weeks 03-06
- Quiz 2 will occur during the lecture in Week 6, on the 20th or 21st of March 2025, and consists of MCQs.
- The Final Project accounts for 50% of the grade and will be released in Week 4
- The Final Project's online submission is due March 23rd, 2025
- The demonstration and presentation of the Final Project will occur during the Lab session in Week 7; contact the Lab Instructor for more details
Course Requirements
- Students must pass both the coursework and final project components to pass the course
- The passing grade is 40/100
Course Content
- Week 1 covers the introduction to CSC1024, problem-solving, programming style, and getting started with Python
- Week 2 covers expressions and variable assignments, inputs/outputs/variables, data types and operations, and error handling/debugging; practical assignments 01 and 02 are related to this week's content
- Week 3 includes decision-making, flowcharts, Python interpreter and text files. Test 01 is on Feb 26th, 2025. There are no physical classes on the 27th and 28th. Practical assignments 03 and 04 are related to week 3's content.
- Week 4 covers iterations (For Loop), nested loops and import library; Practical assignments 05 and project assignments are related to this week's content.
- Week 5 includes Data Structures and Iterations (While Loop); Practical assignments 06 and 07 are related to week 5's content.
- Week 6 covers Functions; Test 02 is on March 20th for Group 1 and March 21st, 2025 for Group 2; Practical assignments 08 and 09 are related to week 6's content.
- Week 7 is a project evaluation with no lecture class
Programming Style
- Programming style involves how you write code; a good style makes code easier to read, debug, and maintain
- Python has PEP 8 guidelines that define the standard style for Python code
- PEP-8 provides documentation for writing beautiful Python code with:
- https://realpython.com/python-pep8/
- https://peps.python.org/pep-0008/
Documentation
- Essential for explaining what code does, why, and how to use it
- Explanatory remarks and comments in a program serve as a guide to help others understand its structure and functionality
- Provides clarity on the purpose of specific code blocks, the logic behind certain decisions, and how different components interact with one another
- Well-written documentation ensures that code is accessible and maintainable
- Comments are ignored during execution and improve readability without affecting the program's functionality
Comments
- In Python, there are two types of comments: single-line and multi-line
- Single-line comments: begin with # and extend to the end of the line
- Single-line comments can be used multiple times to create Multi-line comment
- Multi-line comments or docstrings: use """ or ""'
Tips and Tricks for Commenting
- Begin a program with a statement of purpose and other helpful information for programmers: what the program does, key features, and unique techniques
- Accompany a variable definition with a comment that explains the variable's purpose
- Precede major segments of code with brief comments that explain their purpose
- Include comments to explain the workings of complex or tricky sections of code
Data Types in Python
- Data types define the kind of data that can be stored and manipulated in a programming language
- Everything is treated as an object, and data types specify how the interpreter should handle different types of values
Primitive Data Types
- Python has four primitive data types: integers, floats, strings, and booleans
- Integers (int): all whole numbers (e.g., 1, 0, -3)
- Floats (float): all real numbers (e.g., 7.0, -0.9, 3.142)
- Strings (str): one or more characters (e.g., "Hello world", 'Python is fun!', "56000")
- Booleans (bool): True or False
Integers
- Whole numbers without decimal places
- In real life, the range of integers is infinite
- In Python 3, the data type int is considered unbounded
- Integers are written without commas
Float
- Real numbers have infinite precision
- The digits in the fractional part can continue forever
- Python uses floating-point numbers to represent real numbers
- Python uses 64-bit floats which have 15 to 17 decimal digits of precision, but is constrained by hardware precision
- A floating-point number can be written using either ordinary decimal notation or scientific notation
Boolean
- Values can only be True or False
- Used for logical operations
Strings
- A string literal is a sequence of characters enclosed in single or double quotation marks
- "" and ''' represent the empty string
- Double-quoted strings are useful when dealing with strings that contain quotation marks or apostrophes
Escape Sequences
- An escape sequence in Python is a combination of characters that represents a special character or action
- It is typically used within strings to allow characters that would otherwise be difficult to type or display
- Escape sequences start with a backslash () followed by one or more characters
- \b - Backspace
- \n - Newline
- \t - Horizontal tab
- \ - The \ character
- ' - Single quotation mark
- " - Double quotation mark
String Concatenation and Repetition
- Join one or more strings using the concatenation (+) operator
- Repeat a string a given number of times using the (*) operator
Variables
- A named storage location in computer memory that holds a value
- Variables are fundamental to programming and are used to store and manipulate data
Variable Assignment
- The process of giving a variable a value using the = operator
- A variable has a name (identifier), value, and type
- Variable references use the variable name in expressions after it has been assigned: you can only refer to a variable after its assignment
Variable Naming Rules in Python
- Variable names must begin with a letter (A-Z or a-z) or an underscore (_); spaces are not allowed
- Python's variable names are case-sensitive (name and Name are different)
- Variable names can contain letters, digits (0-9), and underscores (_); the names should not start with a number or other special character
- Reserved words cannot be used as variable names
- The variable name should not be the same as the name of the saved file
Expressions in Python
- A combination of variables, values, and operators that produces a result
- Expressions can be of different types, such as arithmetic, logical, and relational
Arithmetic Expression
- Consists of numeric values, variables, and arithmetic operators that perform calculations
Integer Division
- When dividing one integer by another integer, the result will be an integer and truncate off the decimal part
Float (mixed-mode) Division
- When at least one of the operands is a float/real number, the result will be a float
Type Function
- Used to identify the data type of a value such as int, float, bool, and string
Explicit Type Conversion
- Converting from one data type to another
- int(someVariable)
- float(someVariable)
Logical Expression
- Evaluate conditions and return True or False
- These expressions are used in decision-making and looping
Logical Operators in Python
- and: returns True if both conditions are True
- or: returns True if at least one condition is True
- not: negates the Boolean value
Relational Operators
- Compare values and return True or False
Relational Operators in Python
- ==: Equal to
- !=: Not equal to
- >: Greater than
- <: Less than
- >=: Greater than or equal to
- <=: Less than or equal to
Operator Precedence
- Python follows operator precedence, meaning some operations are executed before others
Order of operator precedence
- () Parentheses - Highest
- ** Exponentiation
- Multiplication, Division, Modulus * , /, //, %
- Addition and subtraction +, -
- Comparison ==, !=, >, <, >=, <=
- Logical NOT not
- Logical AND and
- Logical OR - Lowest
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
This material covers Week 2 of CSC1024 Programming Principles, taught by Dr. Danish Mahmood Khan. Key information includes assessment dates for Quizzes 1 & 2 and the Final Project. Students must pass both coursework and the final project with a minimum grade of 40/100 to pass the course.