Python Variable, Statement and Expression PDF
Document Details
Uploaded by Deleted User
Irwandi Hipiny, Fatin Hanani Kamaluddin & Michelle George
Tags
Summary
This document is a collection of slides discussing Python variables, statements, and expressions. It covers defining variables, naming conventions, different data types such as numbers, strings, and booleans. The document also includes examples of statements and control flow statements.
Full Transcript
Python Variable, Statement and Expression by Irwandi Hipiny, Fatin Hanani Kamaluddin & Michelle George Variable Variable 1 Definition A Python variable is like a container that holds a value, such as a number or a string of text. Think of it as a label you attach to som...
Python Variable, Statement and Expression by Irwandi Hipiny, Fatin Hanani Kamaluddin & Michelle George Variable Variable 1 Definition A Python variable is like a container that holds a value, such as a number or a string of text. Think of it as a label you attach to something so you can easily refer to it later. Dynamic typing - Variables do not need to be declared with any particular type, and can even change type after they have been set. Variable Naming Conventions 1 Rules A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ). A variable name must start with a letter ( A-z ) or the underscore character ( _ ). A variable name cannot start with a number (so 1MDB is a no-no). Variable names are case-sensitive (age, Age and AGE are three different variables). Cannot use Python reserved words (see https://flexiple.com/python/python-reserved-words). 2 Best practices (not mandatory) Use a meaningful name to describe the data a variable holds. Use lowercase letters to name variables. Separate words in variable names with an underscores ( _ ) for readability. Use uppercase letters sparingly, typically for constants or global variables (ALL_CAPS). 1. Scan to open Quiz URL 2. Enter the join code Try me! Legal variables Try me! Illegal variables Data Types in Python Numbers Strings Booleans Python supports integers, Strings are sequences of Boolean values are either floats, and complex characters enclosed in True or False. numbers. quotes. ‘ ‘ or “ “ Remember, Python uses “dynamic typing” – no need to explicitly declare a variable type before using it. Try me! Numbers Try me! Strings and Chars Try me! Booleans Declaring and Assigning Values to Variables 1 2 3 Direct Assignment Multiple Assignment Chained Assignment Try Me! Direct, Multiple and Chained assignment Variable Scoping in Python Local Scope Global Scope Variables defined within a Variables defined outside function are local. any function are global. We’ll re-visit this in Week 14 So.. to recap - Python variable is for storing a value - Python variable are dynamically-typed - Variable naming must follow rules - Python accepts 3 methods for assigning a value to a var - 3 common variable types: numbers, strings, booleans - Scoping: Variables can be local or global (ignore for now Statement Single A block of instruction, instructions, a.k.a a.k.a “computer, “computer, Statement do one thing!” do several things sequentially!” 1. Simple Statement 2. Compound Statement A simple statement is a single A compound statement contains (groups of) other statements. instruction comprised within a single Some affect or control the execution of other statements in some line. way. Can place multiple simple statements Typically, compound statements span multiple lines. in a single line, separate them using ‘ ; Determine the program’s flow: ’ Branching and Looping. 2.1 Control Flow Statement Control flow statements influence the flow of the program's execution. Looping Program Simple Statement Examples Assignment: Assigns a value to a variable. Expression Statements: Executes an expression. Print: Outputs data to the console or file. Try me! Compound Statement Examples for Loop if-elif-else Statement while Loop The for loop is used for Allows for the execution The while loop repeatedly iterating over a of different code based executes a target statement sequence or iterable on whether a certain as long as a given condition It executes a block of object. condition is true or false. is true. It is used when the code for each element in number of iterations is the sequence. unknown. Break and Continue These are keywords used to alter the flow of control in a program. Try me! For loop (note: print() automatically add a newline “\n”) Try me! To print in a single line, add end=“ “ as a parameter in print() Try me! For loop, with a larger increment value Try me! For loop, with a decrement value Try me! For loop, with if - else Try me! While loop Try me! Break and Continue Expressio n Python expression In Python, expressions are combinations of operands and operators that produce a result. Arithmetic Expressions: Operators and Operand Operators Operands Arithmetic expressions use operators such Operands are the values that the operators as +, -, *, /, and % to perform addition, act upon. They can be variables, constants, subtraction, multiplication, division, and or expressions, and are used in arithmetic modulus operations respectively. calculations. Operator Precedence in Python Logical expressions: operators and operands Logical Operators in Python Operands in Logical Expressions Python supports logical operators such as In logical expressions, operands are the AND, OR, and NOT. values or variables that are being compared or evaluated. These operators are commonly used in control flow statements and conditional expressions to make decisions based on multiple conditions. Try me! Arithmetic Expressions Try me! Logical Expressions So.. to recap Python statement can be simple (a single instruction) or mpound-type (a block of statements executed together). ompound-type can be a control-flow statement (i.e., branc d looping). Python expression contains operand(s) and operator(s) Python expression can be arithmetic or logical Q&A