0% found this document useful (0 votes)
4 views15 pages

Session 2

The document outlines the syntax rules and basic concepts of Python programming, emphasizing readability, indentation for code blocks, and the use of comments. It covers variable definitions, naming conventions, data types, operators, and the structure of conditional statements. Additionally, it includes an example of solving a quadratic equation using Python syntax.

Uploaded by

Naveed Malik
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)
4 views15 pages

Session 2

The document outlines the syntax rules and basic concepts of Python programming, emphasizing readability, indentation for code blocks, and the use of comments. It covers variable definitions, naming conventions, data types, operators, and the structure of conditional statements. Additionally, it includes an example of solving a quadratic equation using Python syntax.

Uploaded by

Naveed Malik
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/ 15

Python Syntax

•Set of rules that define how to write Python programs


•Readable: Uses English keywords and indentation
•No punctuation
•Case sensitive
•Reduces errors (missing braces etc.)
•Focuses on logic rather that syntax rules
Code Blocks and Comments
•Uses indentation to define code blocks
•A colon (:) starts a block
•All lines in a block should have the same indentation
•Use spaces or a tab as indentation
•Any number of spaces, but consistently in a single
block
Comments
•Comments are indicated by a sharp sign (#)
•Stand-alone or inline comments are both allowed

• The end of a statement is marked by the end of the


line
Basic Communications
• Print function
• Used for output
• Overloaded for different datatypes so it knows how to handle
them
• (more on print statement later)

• Input function
• Returns a string (always)
• Can be used to prompt for user input
Reserved Words
False def if raise
None del import return
True elif in try
and else is while
as except lambda with
assert finally nonlocal yield
break for not
class form or
continue global pass
Built-in function names are also reserved. E.g print, input etc.
Variables
•What are variables
•Variables created when value assigned; no declaration
•Variables can change types through a new assignment
•The type of a variable can be forced by casting the
RHS
•The simple assignment operator (=)
Variable Naming Conventions
•A variable name must start with a letter or the underscore character
•A variable name cannot start with a number
•A variable name can only contain alpha-numeric characters and
underscores
•(A-z, 0-9, and _ )

•Variable names are case-sensitive (age, Age and AGE are three different
variables)
•A variable name cannot be any of the Python keywords.
•Many values can be assigned to multiple variables: (bad idea)
• x, y, z = 10, "Banana", "Cherry"
Variable Data Types
• str
• int, float, complex
• list, tuple, dict
• set, frozenset
• bool
• bytes, bytearray, memoryview
• NoneType
Operators
•Arithmetic operators
•Assignment operators
•Comparison operators
•Logical operators
•Identity operators
•Membership operators
•Bitwise operators
Arithmetic operators
Addition +
Subtraction -
Multiplication *
Division /

Floor Division //

Modulus %

Exponentiation **
Assignment operators
=
+=
-=
*=
/=
//=
%=
**=
Expressions
• A combination of operators and operands that is
interpreted to produce another value

• E.g.
c = a+b
Comparison operators

• Equals: a == b

• Not Equals: a != b

• Less than: a < b

• Less than or equal to: a <= b

• Greater than: a > b

• Greater than or equal to: a >= b


The if Statement
• Syntax:
if (condition):
statement(s)
elif (some other condition):
statements(s)
else:
statements
Putting a few concepts together

• Solving a quadratic equation:

ax² + bx + c = 0 a≠0

• Roots are
x = [-b±√(b2-4ac)]/2a

You might also like