Basic Syntax of Python
Basic Syntax of Python
PYTHON
IDLE COLOR CODES
• Purple – indicates that you have typed a
command
• Green – specifies the content sent to a
command
• Blue – shows the output from a command
• Black – Defines non-command entries
PYTHON TWO BASIC MODES
• Interactive Mode Programming
- Is a command line shell which gives immediate feedback for each
statement, while running previously fed statements in active
memory.
• Click File
• Click New File
• output for the
program House.py:
Python Identifiers
• Is a name used to identify a variable, function, class,
module or other object.
• Starts with a letter A to Z or an underscore(_) followed
by zero or more letters, underscores and digits (0 to
9).
• Must not contain any white-space, or special
character (!, @, #, %, ^, &, *)
• Must not similar to any keyword defined in the
language.
• Identifier names are case sensitive: myName and
MyName are different.
• Examples of valid identifiers: a123, _n, n_9 etc.
• Examples of Invalid identifiers: 1a, n%4, n 9, etc.
Lines and Indentions
• 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.
Multi-Line Statements
• Statements in Python typically end with a new
line.
• Python does, however allow the use of the line
continuation character (\) to denote that the line
should continue.
Comments in Python
• Comments are ignore by the python interpreter.
• A hash sign (#) that is not inside a string literal
begins a comment. Use for single line comment
• Triple-quoted string (‘’’) is also ignored by the
python interpreter and can be used as a
multiline comments.
Assigning Variables
• Used equal sign (=) to assign a value to a
variable.
• Ex.
• Name=“Devan”
• num1=20
• name = “Rose”
Multiple Assignment
• Assigning single value to multiple variables:
x=y=z=50
print (x)
print (y)
print (z)
Output:
50
50
50
Multiple Assignment
• Assigning multiple values to multiple variables:
a,b,c=5,10,15
print a
print b
print c
Output
5
10
15
Data Types according to Categories
• Text Type: str
• Numeric Types: int, float, complex
• Sequence Types: list, tuple, range
• Mapping Types:dict
• Set Types: set, frozenset
• Boolean Type: bool
• Binary Types: bytes, bytearray, memoryview
Note: You can get the data type of any object by using the type()
function.
BASIC OPERATORS
Types of Operator
• Arithmetic Operators – are used to perform arithmetic operations
between two operands.
• Comparison Operators – are used to comparing the value of the two
operands and returns Boolean true or false accordingly
• Logical Operators – are used primarily in the expression evaluation
tom make a decision.
Arithmetic Operators
Operator Description
+ (Addition) It is used to add two operands.
/ (Division) It returns the quotient after dividing the first operand by the second operand.
% (remainder) It returns the remainder after dividing the first operand by the second operand.
// (Floor division) It gives the floor value of the quotient produced by dividing the two operands
Comparison Operator
Operator Description
== If the value of two operands is equal, then the condition becomes true.
!= If the value of two operands is not equal, then the condition becomes true.
<= If the first operand is less than or equal to the second operand, then the
condition becomes true.
>= If the first operand is greater than or equal to the second operand, then the
condition becomes true.
> If the first operand is greater than the second operand, then the condition
becomes true.
< If the first operand is less than the second operand, then the condition becomes
true.
Logical Operators
Operator Description
and (&&) If both the expression are true, then the condition will be
true.
or (||) If one of the expressions is true, then the condition will be
true.
not (!) If an expression a is true, the not (a) will be false and vice
versa
Input() command
• Allows the user to input certain values
• Ex.
a=input()
b=input(‘Enter No:’)