MUH101: Python Programming Basics
31 Questions
0 Views

MUH101: Python Programming Basics

Created by
@CelebratedJasper5057

Podcast Beta

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What will be the result of the expression int(3.9)?

  • 3 (correct)
  • 3.9
  • 4
  • ValueError
  • Which of the following statements about type conversion is true?

  • Converting a string that cannot be interpreted as a number will cause an error. (correct)
  • Using `float()` on a string will always succeed.
  • All types are convertible without restrictions.
  • Integers can only be converted to floats using explicit methods.
  • What is the output of the command print(3 + 2)?

  • 5 (correct)
  • 3.5
  • 3 + 2
  • 5.0 (correct)
  • Which of the following expressions will yield a float as a result?

    <p>4 * float(2)</p> Signup and view all the answers

    What does the print() function do?

    <p>It outputs text or values to the console.</p> Signup and view all the answers

    What type of error occurs when a program runs without crashing but produces an unexpected output?

    <p>Logical error</p> Signup and view all the answers

    What is the result of executing the command 'print(3 + 'a')'?

    <p>TypeError</p> Signup and view all the answers

    Which of the following is an example of a scalar object in Python?

    <p>5.0</p> Signup and view all the answers

    How can you check the type of an object in Python?

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

    What happens when a program encounters a syntactic error?

    <p>It generates a message indicating invalid syntax.</p> Signup and view all the answers

    What is a non-scalar object in Python?

    <p>A string</p> Signup and view all the answers

    When does a static semantic error occur?

    <p>When a type mismatch occurs that cannot be checked until runtime.</p> Signup and view all the answers

    Which statement about objects in Python is true?

    <p>Objects have a type that dictates how they can be used.</p> Signup and view all the answers

    What is the first step in the process of finding a guess g for which g ∗ g is close to x?

    <p>If g ∗ g is close enough to x, stop.</p> Signup and view all the answers

    Which best describes an algorithm in the context of programming?

    <p>A sequence of simple steps combined with control flow to determine when to stop.</p> Signup and view all the answers

    What type of computer stores and executes a sequence of instructions?

    <p>Stored program computer</p> Signup and view all the answers

    Which of the following best describes static semantics in programming languages?

    <p>The meaning assigned to syntactically valid strings.</p> Signup and view all the answers

    Which of the following statements about programming languages is true?

    <p>Any computable algorithm can be executed in any programming language.</p> Signup and view all the answers

    What result does the expression '3 + '5'' produce in a programming language?

    <p>Static semantic error</p> Signup and view all the answers

    What is a basic primitive in programming, as described by Turing?

    <p>A single operation or action that can be executed.</p> Signup and view all the answers

    Which of the following provides a means of determining when to stop in a recipe-like process?

    <p>Flow of control</p> Signup and view all the answers

    How do expressions in a programming language differ from basic primitives?

    <p>Expressions consist of complex combinations of primitives.</p> Signup and view all the answers

    In programming, what does semantics refer to?

    <p>The interpretation and meaning of a syntactically correct statement.</p> Signup and view all the answers

    What is the result of the expression $3 * (2 + 5)$?

    <p>21</p> Signup and view all the answers

    What does the expression $i , ext{mod} , j$ represent?

    <p>The remainder when $i$ is divided by $j$</p> Signup and view all the answers

    What will be the value of $a$ after executing the command $a, b = 3, 5$?

    <p>3</p> Signup and view all the answers

    According to operator precedence, which operation will be executed first in the expression $3 + 5 * 2$?

    <p>Multiplication ($5 * 2$)</p> Signup and view all the answers

    What does the syntax '# This is a comment' do in Python?

    <p>Is ignored by Python</p> Signup and view all the answers

    Which of the following correctly represents an assignment of multiple variables?

    <p>x = y = 4</p> Signup and view all the answers

    What will be the output if the value of pi is set to 3.14?

    <p>3.14</p> Signup and view all the answers

    What is the outcome of the expression $3 ** 4$?

    <p>81</p> Signup and view all the answers

    Study Notes

    Course Details

    • MUH101: Learn Python programming language
    • All classes are face-to-face
    • Communication is through Hacettepe University Digital Learning Platform (HADİ)

    Grading

    • 60% Midterm Exam (2 exams)
    • 40% Final Exam (1 exam)
    • Midterm format will be announced during the semester

    Policy

    • Collaboration on course material is encouraged
    • Exams must be done individually, cheating results in -100, F3, and disciplinary action.

    Programming Tips

    • The course is fast-paced, practice early
    • Do not skip lectures
    • For beginners, practice is key.
    • Download code before lecture and follow along.
    • Don't be afraid to experiment with Python commands.

    Recipes and Algorithms

    • A recipe is a sequence of simple steps
    • It defines the flow of control process and when each step is executed
    • It determines when to stop
    • An algorithm is similar to a recipe
    • Example of Algorithm: 1 + 2 + 3 = an algorithm!

    Computers as Machines

    • Computers are machines that capture recipes as mechanical processes.
    • Fixed program computer: A calculator, performs specific functions and has a fixed programming
    • Stored program computer: Stores and executes instructions, more advanced than fixed program computers

    Stored Program Computers

    • Instructions are stored inside the computer.
    • Instructions are built from a set of primitive instructions: arithmetic, logic, simple tests, and data movement.
    • A special program called an interpreter executes instructions in order.
    • Tests in the program alter the flow of control within the instruction sequence.
    • The process stops when all instructions are executed.

    Basic Primitives

    • Turing proved that any computation can be done with 6 primitive instructions.
    • Modern programming languages offer a broader and more convenient set of primitives.
    • New primitives can be created. 
    • “Anything computable in one language is computable in any other programming language.” - Python, Java, C, Pascal, C++, and C# function similarly.

    Creating Recipes

    • Programming languages provide primitives: numbers, strings, simple operators
    • Expressions are legal combinations of primitives within a programming language.

    Aspects of Languages

    • Syntax: Rules for valid strings (grammar)
    • Static Semantics: Rules for which syntactically valid strings have meaningful interpretations
    • Semantics: Meaning associated with syntactically correct strings

    Where Things Go Wrong

    • Syntactic errors: Common, easily caught, errors in the grammar of the code
    • Static semantic errors: Some languages catch these before running. Can cause unpredictable behavior
    • No static semantic errors, but unintended meaning: Can cause crashes, infinite loops, and unexpected results.

    Examples

    • Syntactic error: print(3.a), a = 5; 3a + 2, a - 3'a', Python will throw a "SyntaxError"
    • Static semantic error: 3 + 'a', Python will throw a "TypeError"

    Python Programs

    • Program is a sequence of definitions and commands
    • Definitions are evaluated, commands are executed by the Python interpreter within a shell.
    • Commands can be typed directly into a shell or stored in a file that is read into the interpreter.

    Objects

    • Objects have a type that defines what a program can do with them.
    • Scalar objects: Cannot be subdivided. Examples include integers (int), real numbers (float), Boolean values (bool), and NoneType.
    • Non-scalar objects: Have internal structure. Examples include Strings.

    Scalar Objects

    • int represents Integers. Example: 5
    • float represents Real numbers. Example: 3.27
    • bool represents Boolean values. Example: True, False
    • NoneType represents a special value called None.
    • type() function can be used to determine the type of an object.

    Non-Scalar Objects

    • Strings: Sequence of Characters, have internal structure. Example: name = "burkay"
    • name[2:4] would call out elements 2 and 3.
    • Object-oriented programming centers around using non-scalar objects.

    Type Conversions (Cast)

    • Convert objects from one type to another but not all types are convertible.
    • float(3) Converts integer 3 to float 3.0
    • int(3.9) Truncates float 3.9 to integer 3

    Printing to Console

    • Use print() command to display output from code to a user.

    Expressions

    • Combine objects & operators to create expressions.
    • Expressions have values and types.

    Operators on ints and floats

    • i+j: Sum of integers i and j. Result is an integer if both are int, else is a float.
    • i-j: Difference
    • i*j: Product
    • i/j: Division, result is always a float
    • i%j: Remainder when i is divided by j
    • i**j: i raised to the power of j

    Simple Operations

    • Parentheses determine order of operations, 3 * (2 + 5)
    • Order of operations without parentheses: **, *, / , + and - executed left to right.

    Binding Variables and Values

    • = assigns a value to a variable name.
    • pi = 3.14159 binds pi to 3.14159 which is stored in memory.
    • Retrieve value associated with a variable by invoking its name, by typing pi.

    Multiple Assignments

    • Assign multiple values to multiple variables simultaneously.
    • a, b = 3, 5

    Variable Naming

    • Use clear, understandable names.
    • Parentheses make the code more readable.

    Comments

    • Adds enrichment and documentation to code.
    • Start a line with # to denote a comment. Python ignores commented lines.

    Abstracting Expressions

    • Why give names to values of expressions?
    • It helps you organize and manage your code.
    • It improves the readability and maintainability of your program.
    • It allows you to reuse values and expressions without repeating them.
    • Overall, it contributes to writing efficient and well-structured code.

    Studying That Suits You

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

    Quiz Team

    Related Documents

    Description

    Explore the fundamentals of Python programming in this interactive quiz designed for MUH101 students. Test your understanding of core concepts, algorithms, and programming tips to excel in your courses. Join us to enhance your skills and prepare for exams!

    More Like This

    Use Quizgecko on...
    Browser
    Browser