Python Variables, Data Types, Operators, Functions, and Control Flow
6 Questions
2 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 is NOT a valid variable name in Python?

  • my_variable
  • _myVariable
  • 2nd_variable (correct)
  • myVar_2
  • What is the correct way to assign a value of 3.14 to a variable called 'pi' in Python?

  • pi = '3.14'
  • pi: 3.14
  • set pi as 3.14
  • pi = 3.14 (correct)
  • Which data type in Python is used to store whole numbers?

  • string
  • float
  • integer (correct)
  • boolean
  • If you want to concatenate two strings in Python, which operator should you use?

    <ul> <li></li> </ul> Signup and view all the answers

    What is the output of the expression $5(7 + 3)$ in Python?

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

    Which of the following arithmetic operators in Python computes the remainder of a division?

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

    Study Notes

    Variables in Python

    In Python, a variable holds a value and is assigned with a name for reference. Names follow certain rules such as starting with a letter or underscore, containing alphanumeric characters plus underscores, and being case-sensitive. Assign values to variables using variable_name = value. To change the value later, simply assign a different value to the existing variable. Variables come in various forms depending on the type of data stored—integers, floats, strings, boolean, lists, and tuples are among the most commonly encountered.

    Here is an example of declaring and changing the value of a variable called age storing a whole number.

    ## Declare the variable age
    age = 42
    
    print("Current age:", age)
    
    ## Change the age to 45
    age = 45
    
    print("\nNew age:", age)
    

    Output: Current age: 42 New age: 45

    Data Types in Python

    Data types determine how data is interpreted and manipulated. Common data types include integers, floating point numbers, strings, booleans, and objects for structured data like lists and tuples. Each data type has its associated operations.

    For instance, integer arithmetic supports addition (+), subtraction (-), multiplication (*), division (/) and floor division (%), whereas string components can be accessed via indexing.

    Operators in Python

    Operators perform mathematical or logical actions on operands, resulting in a value. Arithmetic operators like +, -, *, / handle basic calculations. Comparison operators like >, <, ==, != compare values and evaluate to True or False according to the result. Logical operators and, or, not combine conditions with Boolean logic. Bitwise operators perform bit-by-bit operations on integers.

    Consider the expression:

    a = 4
    b = 2
    result = a + b
    
    print(result)
    

    Output: 6

    Here, the + operator adds the values of a and b.

    Functions in Python

    Functions define blocks of code that can be repeatedly used, accepting parameters and returning a value. They help modularize application design.

    Define a factorial function, factorial(num), that returns the factorial of a non-negative integer number num. Factorials calculate the product of all positive integers less than or equal to num.

    def factorial(num):
        '''Calculate factorial'''
        if num == 0:
            return 1
        elif num > 0:
            return num * factorial(num-1)
    
    ## Example usage
    print("Factorial of 5 is:", factorial(5))
    

    Output: Factorial of 5 is: 120

    Control Flow in Python

    Control flow manages the execution order of statements within loops or conditionals. Loops iterate until a termination criterion is met, while conditional statements select paths based on truth-values.

    Below is a simple loop printing odd numbers between 1 and 10 using a while statement.

    i = 1
    while i <= 10:
        print(i)
        i += 2
    

    Output: 1 3 5 7 9

    Studying That Suits You

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

    Quiz Team

    Description

    Learn about Python variables, data types like integers and strings, operators including arithmetic and logical operators, functions for code modularity, and control flow for managing statement execution. Explore examples of declaring/changing variables, using arithmetic operators, defining functions like factorial, and implementing control flow with loops and conditionals.

    More Like This

    Use Quizgecko on...
    Browser
    Browser