Python Programming 1 Basic Syntax Python Identifiers A
Python Programming (1) Basic Syntax
Python Identifiers A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9). Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive programming language.
Python Keywords These are reserved words and can not be used as constant or variable or any other identifier names. All the python keywords contain lowercase letters only. and exec not assert finally or break for pass class from print continue global raise def if return del import try elif in while else is with except lambda yield
Python Variables Python uses the equal sign (=) to assign values to variables. The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable. Python has five standard data types Numbers String List Tuple Dictionary
Lines and Indentation Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced. The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. Thus, in Python all the continuous lines indented with same number of spaces would form a block.
Types of Operators Python language supports the following types of operators. Arithmetic Operators Comparison (Relational) Operators Assignment Operators Logical Operators Bitwise Operators Membership Operators Identity Operators
Python Arithmetic Operators Operator Operation Description + Addition Add values on either side of the operator - Subtraction Subtract right hand operand from left hand operand * Multiplication Multiplies values on either side of the operator / Division Divides left hand operand by right hand operand % and returns remainder Modulus Divides left hand operand by right hand operand ** operands Exponent Performs exponential (power) calculation on
Python Comparison Operators Operator Description == If the values of two operands are equal, then the condition becomes true != If the values of two operands are not equal, then condition becomes true <> If the values of two operands are not equal, then condition becomes true > If the value of left operand is greater than the value of right operand, return true < If the value of left operand is less than the value of right operand, return true >= return true If the value of left operand is greater than or equal to the value of right operand, <= true If the value of left operand is less than or equal to the value of right operand, return
Python Assignment Operators Operator Description = Assign values from right side operands to left side operand += It adds right operand to the left operand assign the result to the left operand -= operand It subtracts right operand from the left operand assign the result to the left *= operand It multiplies left operand with the right operand assign the result to the left /= operand It divides left operand with the right operand assign the result to the left %= It takes modulus using two operands and assign the result to the left operand
for loop Syntax for iterating_var in sequence: statements(s) If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assigned to the iterating variable iterating_var. Next, the statements block is executed. Each item in the list is assigned to iterating_var, and the statement(s) block is executed until the entire sequence is exhausted. Flow Diagram
Python Functions A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. Syntax def functionname( parameters ): "function_docstring" function_suite return [expression]
- Slides: 11