Python Constants, Variables, and Reserved Words

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 is the most accurate description of a 'constant' in programming?

  • A value that changes every time the program is executed.
  • A reserved keyword used for mathematical operations.
  • A named memory location that stores data.
  • A fixed value, such as a number, letter, or string, whose value does not change during program execution. (correct)

Why are certain words like if, else, and while designated as 'reserved words' in Python?

  • To prevent them from being used as variable names, as they have predefined meanings in the language's syntax. (correct)
  • To allow programmers to use them freely as variable names.
  • To serve as comments within the code.
  • To indicate the end of a program.

Which of the following statements best describes a 'variable' in programming?

  • A fixed value that cannot be altered during program execution.
  • A keyword used to define functions.
  • A data type that represents only numeric values.
  • A named storage location in memory that can hold a value, which can be changed during program execution. (correct)

Which of the following demonstrates a valid variable name in Python?

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

Given the Python code: x = 35.0 y = 12.50 z = x * y print(z) What will be the output of this code?

<p><code>437.5</code> (B)</p> Signup and view all the answers

What is the primary function of an 'assignment statement' in Python?

<p>To assign a value to a variable. (A)</p> Signup and view all the answers

Consider the following Python assignment statement: y = x + 2. What does this statement do?

<p>It calculates the value of <code>x + 2</code> and assigns the result to the variable <code>y</code>. (B)</p> Signup and view all the answers

Given the following expression in Python: x = 3.9 * x * (1 - x) If x initially holds the value 0.6, what is the new value of x after this statement is executed?

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

According to operator precedence rules, which operation is performed first in the following Python expression? x = 1 + 2 * 3 - 4 / 5 ** 6

<p>Exponentiation (<code>**</code>) (C)</p> Signup and view all the answers

Consider the expression: x = 1 + 2 ** 3 / 4 * 5. According to Python's order of operations, what is the value of x?

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

In Python, what does it mean when we say a variable has a specific 'type'?

<p>The variable can only store one kind of data (e.g., number, text). (B)</p> Signup and view all the answers

Which of the following is an example of implicit type conversion in Python?

<p>Adding an integer to a floating-point number, resulting in a floating-point number. (B)</p> Signup and view all the answers

What will be the result of the code print(10 / 2) in Python 3?

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

What happens if you try to convert a string containing non-numeric characters (e.g., 'hello') to an integer using the int() function in Python?

<p>Python will raise a ValueError. (D)</p> Signup and view all the answers

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

<p>To receive data from the user via the console. (B)</p> Signup and view all the answers

If you prompt the user for a number using the input() function, what data type is returned?

<p><code>str</code> (B)</p> Signup and view all the answers

In Python, what is the purpose of comments?

<p>They are ignored by the Python interpreter and are used to explain the code. (C)</p> Signup and view all the answers

Which symbol is used to denote a comment in Python?

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

Which of the following code snippets correctly prompts the user for their name and then prints a greeting?

<p><code>name = input('Enter your name:')</code> <code>print('Hello', name)</code> (A)</p> Signup and view all the answers

Given the code: inp = input('Europe floor?') usf = int(inp) + 1 print('US floor', usf) If the user enters 0, what will be printed?

<p>US floor 1 (D)</p> Signup and view all the answers

What is the missing code? a = 5 b = 6 ___ a = b b = c

<p><code>c = a</code> (B)</p> Signup and view all the answers

What will happen when the following code is executed in Python? x = '123' y = x + 1 print(y)

<p>The code will raise a TypeError because you can't add a string and an integer. (C)</p> Signup and view all the answers

What will be the output of the following Python code? xx = 23 kk = xx % 5 print(kk)

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

Given the code: yy = 440 * 12 zz = yy / 1000 print(zz) What is the final output?

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

Given the python code: print(float(99) + 100) What is the result?

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

Given the code, what is the result? i = 42 print(type(i))

<p><code>class 'int'</code> (D)</p> Signup and view all the answers

Given the code: i = 42 f = float(i) print(f) What is printed to the console?

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

What is the printed to the console after running the code: print(10 // 2)?

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

What is the printed to the console after running the code: sval = '123' print(sval + 1)?

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

Which python code will prompt somebody to enter their name?

<p><code>name = input('Please enter your name')</code> (D)</p> Signup and view all the answers

If the user enters 0 when running the code inp= input('Europe floor?') usf = int(inp) + 1 print('US floor', usf) what will be printed?

<p><code>US floor 1</code> (C)</p> Signup and view all the answers

Which line of code contains a comment?

<p><code>#This is a comment</code> (B)</p> Signup and view all the answers

If you wanted to know the amount of characters somebody typed into the console as their name, what code would you implement? Assume they already answered the input.

<p><code>len(name)</code> (B)</p> Signup and view all the answers

If you wanted to assign a to equal b, and b to equal a, what code could you use?

<p><code>c = a</code> <code>a = b</code> <code>b = c</code> (B)</p> Signup and view all the answers

Flashcards

Constants

Fixed values like numbers, letters, and strings.

Reserved Words

Words that have predefined meanings in Python and cannot be used as variable names.

Variable

A named memory location where data can be stored and retrieved.

Python Variable Naming Rules

Rules that dictate how variables are named in Python.

Signup and view all the flashcards

Statements

Instructions that the Python interpreter can execute.

Signup and view all the flashcards

Assignment Statement

A statement that assigns a value to a variable.

Signup and view all the flashcards

Expression

A combination of values, variables, and operators that Python evaluates to produce a result.

Signup and view all the flashcards

Operator Precedence

The order in which operators are evaluated in an expression.

Signup and view all the flashcards

Type

The category or type of value a variable can hold (e.g., integer, string).

Signup and view all the flashcards

Integers

Whole numbers (e.g., -14, 0, 1, 100).

Signup and view all the flashcards

Floating Point Numbers

Numbers with a decimal point (e.g., -2.5, 0.0, 98.6).

Signup and view all the flashcards

Type Conversion

Converting a value from one type to another (e.g., integer to float).

Signup and view all the flashcards

Integer Division

Division that results in an integer, discarding any remainder.

Signup and view all the flashcards

User Input

The function input() pauses execution and reads data from the user.

Signup and view all the flashcards

Comments

Text added to code that is ignored by Python, used for explanation or documentation.

Signup and view all the flashcards

Study Notes

Constants

  • Fixed values such as numbers, letters and strings are called constants because their value does not change
  • Numeric constants are as expected
  • String constants use either single quotes (') or double quotes (")

Reserved Words

  • Reserved words cannot be used as variable names or identifiers
  • Examples: False, None, True, and, as, assert, break, class, etc.

Variables

  • A variable is a named memory location where a programmer can store data
  • The data can be retrieved later by using the variable's name
  • Programmers can choose the name of the variables
  • The contents of a variable can be changed in a later statement

Python Variable Name Rules

  • Variable names must start with a letter or underscore
  • Variable names must consist of letters, numbers, and underscores
  • Variable names are case-sensitive
  • Reserved words cannot be used as variable names

Sentences or Lines

  • Sentences or lines can be classified as assignment, assignment with expression or print statements
  • x = 2 is an assignment statement
  • y = x + 2 is an assignment with expression
  • print(y) is a print statement
  • The components of a line or sentence are variables, operators, constants and functions

Assignment Statements

  • Values are assigned to variables, using the = assignment statement
  • An assignment statement consists of an expression on the right-hand side, and a variable to store the result

Expressions

  • Lack of mathematical symbols on computer keyboards requires "computer-speak" to express classic math operations

Numeric Expressions

  • + is used for addition
  • - is used for subtraction
  • * is used for multiplication
  • / is used for division
  • ** is used for power
  • % is used for remainder

Order of Evaluation

  • Python must know which operation to perform first when stringing operators together
  • This is known as "operator precedence"

Operator Precedence Rules

  • Highest precedence rule to lowest precedence rule:
    • Parentheses are always respected
    • Exponentiation (raise to a power)
    • Multiplication, Division, and Remainder
    • Addition and Subtraction
    • Left to right

Type

  • Variables, literals, and constants have a "type" in Python
  • Python recognizes the difference between an integer number and a string
  • The plus sign (+) indicates "addition" if something is a number, and "concatenate" if the thing is a string

String Concatenation

  • String concatenation is putting strings together

Type Matters

  • Python automatically recognizes the variable type
  • Certain operations are prohibited
  • It isn't possible to add a number to a string
  • type() determines the variable type

Types of Numbers

  • Numbers can be either int or float
  • Integers are whole numbers, and have the type int
  • Floating point numbers have decimal parts, and have the type float

Type Conversions

  • An integer is implicitly converted to a float, if an expression contains both an integer and a floating point number
  • The built-in functions int() and float() can control this conversion

Integer & Float Division

  • Integer division result uses the // operator
  • Floating point result uses the / operator
  • There was a different behavior in Python 2.x

String Conversion

  • Use int() and float() to convert between strings and integers
  • An error occurs if the string does not contain numeric characters

User Input

  • Python pauses and reads user data entered using the input() function
  • input() returns a string

Converting User Input

  • int() is used to convert user input from a string to a number

Comments in Python

  • Use # to comment out lines of code, as Python ignores anything after it
  • Comments are used to describe code, document the author, or turn off a line of code

Coding Exercise 1: Band Generator Game

  • Create a greeting for the program
  • Ask the user for the city that they grew up in
  • Ask the user for the name of a pet
  • Combine the names of the user's city and pet to show them their band name
  • Ensure the input cursor shows on a new line
  • Use escape sequences

Coding Exercise 2: Correct Errors

  • Correct the errors for display to match these lines
    • Day 1 - String Manipulation
    • String Concatenation is done with the "+" sign.
    • e.g., print("Hello " + "World")
    • New lines can be created with a backslash and n.

Coding Exercise 3: Input Function and len Function

  • Google to find a function that calculates the length of a string
  • Write a program that prints the number of characters of a user's name

Coding Exercise 4: Swapping Values

  • Write a program that switches the values stored in the variables a and b
  • e.g. swap variable a = 5 and b = 6, to a = 6 and b = 5

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Python Variables and Data Types Quiz
5 questions
Python Variables Basics Quiz
5 questions
Python Program: Student Bio-data
8 questions
Use Quizgecko on...
Browser
Browser