CSC1024 Programming Principles: Week 02
37 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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?

  • 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?

  • 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?

<p>They can be created using multiple single-line comments but are typically created using triple single quotes (<code>'''</code>) or triple double quotes (<code>&quot;&quot;&quot;</code>). (B)</p> Signup and view all the answers

Which of the following is NOT a valid guideline for writing self-explanatory and maintainable code?

<p>Using cryptic and abbreviated variable names to reduce code size (C)</p> Signup and view all the answers

What consideration should be taken when choosing variable names in Python?

<p>Variable names should be self-explanatory and meaningful. (D)</p> Signup and view all the answers

In Python, which data type is used to represent whole numbers without any decimal places?

<p>integer (A)</p> Signup and view all the answers

What distinguishes a float data type from an int data type in Python?

<p><code>float</code> represents real numbers with decimal points, while <code>int</code> represents whole numbers. (D)</p> Signup and view all the answers

Which data type would you use to store a value that represents whether a condition is true or false?

<p>boolean (A)</p> Signup and view all the answers

How are string literals represented in Python?

<p>Enclosed in single or double quotes. (A)</p> Signup and view all the answers

What is the purpose of escape sequences in Python strings?

<p>To represent characters that are difficult to type or display directly. (A)</p> Signup and view all the answers

Which escape sequence is used to insert a newline character into a Python string?

<p>\n (C)</p> Signup and view all the answers

What is the operation called when you combine two or more strings in Python?

<p>Concatenation (A)</p> Signup and view all the answers

Which operator is used for string concatenation in Python?

<ul> <li>(C)</li> </ul> Signup and view all the answers

What is a variable in Python?

<p>A named storage location in computer memory that holds a value. (C)</p> Signup and view all the answers

What is the primary purpose of variable assignment in Python?

<p>To give a variable a specific value that it will store. (C)</p> Signup and view all the answers

Which of the following is NOT a valid rule for naming variables in Python?

<p>Variable names can start with a number if it is a floating-point variable.. (C)</p> Signup and view all the answers

Given the expressions A = 10, B = 20, and then C = A + B, what will C store?

<p>30 (C)</p> Signup and view all the answers

Which of the following variable names is invalid in Python?

<p>1name (C)</p> Signup and view all the answers

What is an expression in Python?

<p>A combination of variables, values, and operators that produces a result. (B)</p> Signup and view all the answers

What does an arithmetic expression consist of?

<p>Numeric values, variables, and arithmetic operators. (D)</p> Signup and view all the answers

In Python, what is the result of the expression 10 // 3?

<p>3 (C)</p> Signup and view all the answers

If you divide one integer by another integer using the // operator, what type of result will you obtain?

<p>integer (D)</p> Signup and view all the answers

What will be the data type of the variable result after executing result = 5 / 2.0?

<p>float (A)</p> Signup and view all the answers

Given the code x = 1 / 2.0, what value will x be assigned?

<p>0.5 (A)</p> Signup and view all the answers

What is the primary purpose of the type() function in Python?

<p>To determine the data type of a variable. (D)</p> Signup and view all the answers

Which function is used for explicit type conversion in Python?

<p>int(), float(), str(), etc. (D)</p> Signup and view all the answers

If you have a float variable my_float = 3.143, how would you convert it to an integer?

<p>int(my_float) (A)</p> Signup and view all the answers

What happens when you try to convert a string that contains letters (e.g., 'a') into an integer using int()?

<p>A ValueError is raised. (A)</p> Signup and view all the answers

What do logical expressions evaluate to in Python?

<p>True or False. (C)</p> Signup and view all the answers

Which logical operator returns True only if both conditions are True?

<p>and (A)</p> Signup and view all the answers

What is the purpose of relational operators in Python?

<p>To compare values and return True or False. (D)</p> Signup and view all the answers

In Python, what does the operator == do?

<p>Compares if two values are equal. (C)</p> Signup and view all the answers

Given the expression (5 > 3) and (10 < 5), what will it evaluate to?

<p>False (A)</p> Signup and view all the answers

Which of the following represents the correct order of operator precedence in Python (from highest to lowest)?

<p>Parentheses, Exponentiation, Multiplication/Division, Addition/Subtraction (D)</p> Signup and view all the answers

Based on operator precedence, what is the result of the expression 5 + 2 * 3?

<p>11 (A)</p> Signup and view all the answers

What is the output of the following Python code?

print(type(5))

<p>&lt;class 'int'&gt; (D)</p> Signup and view all the answers

Flashcards

Programming Style

The way you write code; good style makes code easier to read and maintain.

Documentation

Explanatory remarks in code helping others understand its structure and functionality.

Comments in Python

Used to explain code;ignored by the interpreter. Single-line with #, multi-line with triple quotes.

Escape Sequences

A combination of characters representing special actions within strings like newline or tab.

Signup and view all the flashcards

String Operations

Joining strings (+) or repeating them (*)

Signup and view all the flashcards

Variable Definition

A named storage location in memory holding a value that can be changed.

Signup and view all the flashcards

Variable Assignment

The process of assigning a value to a variable using the = operator.

Signup and view all the flashcards

Variable Naming Rules

Must start with a letter or underscore; case-sensitive; no spaces or reserved words.

Signup and view all the flashcards

Expression Definition

A combination of variables, values, and operators that produces a result.

Signup and view all the flashcards

Arithmetic Expression

Numeric values/variables with operators (+,-,*,/,%,**,//) for calculations.

Signup and view all the flashcards

Integer Division (//)

Divides integers, result is an integer, truncating the decimal part.

Signup and view all the flashcards

Float Division (/)

Division where at least one operand is float, result is a float.

Signup and view all the flashcards

Type Function

A function to determine the data type of a value or variable.

Signup and view all the flashcards

Explicit Type Conversion

Converting from one data type to another (e.g., int(), float()).

Signup and view all the flashcards

Logical Expression

Expressions evaluating conditions returning True or False; used in decision-making.

Signup and view all the flashcards

Relational Operators

In Python, compares values and returns True or False.

Signup and view all the flashcards

Operator Precedence

Defines the order of operator execution. Parentheses first, etc.

Signup and view all the flashcards

Data Types

Kind of data that can be stored and manipulated.

Signup and view all the flashcards

Integer (int)

All whole numbers (1, 0, -3).

Signup and view all the flashcards

Float

All real numbers (7.0, -0.9, 3.142).

Signup and view all the flashcards

String (str)

One or more characters

Signup and view all the flashcards

Boolean

True or False logic

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.

Quiz Team

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.

More Like This

Use Quizgecko on...
Browser
Browser