0% found this document useful (0 votes)
217 views24 pages

Basic Syntax of Python

The document summarizes basic Python syntax and concepts in three parts: 1. It describes Python's interactive and script modes of programming and how code is run differently in each. It also covers basic syntax elements like indentation, comments, and identifiers. 2. It explains Python's core data types and basic operators for arithmetic, comparison, and logic. It provides examples of using operators on different data types. 3. It gives an overview of common programming concepts like variables, assignments, input/output, and control flow structures and how they are implemented in Python.

Uploaded by

Benedict Aure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
217 views24 pages

Basic Syntax of Python

The document summarizes basic Python syntax and concepts in three parts: 1. It describes Python's interactive and script modes of programming and how code is run differently in each. It also covers basic syntax elements like indentation, comments, and identifiers. 2. It explains Python's core data types and basic operators for arithmetic, comparison, and logic. It provides examples of using operators on different data types. 3. It gives an overview of common programming concepts like variables, assignments, input/output, and control flow structures and how they are implemented in Python.

Uploaded by

Benedict Aure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

BASIC SYNTAX OF

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.

• Script Mode Programming


- The normal mode where the scripted and finished .py files are run
in the Python interpreter
Interactive Mode Programming
• Write the program
\n – new line
+ - for concatenate

• Click File then Save or CTRL + S


• Name your file with .py
extension. Click Save

• Notice the title Bar


• Click Run or F5
Script Mode Programming

• 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.

- (Subtraction) It is used to subtract the second operand from

* (Multiplication) It is used to multiply one operand with the other

/ (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.

** (Exponent) It calculates the first operand power to 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:’)

You might also like