2. Identifier
โธ Is a name given to a variable, function, class or module.
โธ Can only contain letter and number and underscore.
โธ my_list_11
โธ They can start just with letter or underscore.
โธ Space are not allows in variable name.
โธ use underscore instead
โธ Python keywords can not be used as identi๏ฌers.
โธ Python is case sensitive.
โธ โNameโ and โnameโ are 2 different identi๏ฌers!
3. โธ Always use meaningful name for identi๏ฌer.
Identifier
Identi๏ฌer Valid Not Valid
Good Bad
name
17name
%name
x
students_list
x-list
for
teacher id
Best Practice
Keyword
space
4. Keywords
โธ Keywords are a list of reserved words that have prede๏ฌned meaning.
Python Keywords
def with in True
return for is False
class while and None
break if or global
continue elif not nonlocal
try else as from
except pass import
๏ฌnally assert raise
5. Statements and Expression
โธ A Statement is an Instruction that the Python Interpreter can execute.
โธ welcome_message=โWelcome To Pythonโ
โธ A Python Program consist of a sequence of statements.
โธ An statement can be one or several line of code.
โธ Expression is an arrangement of values and operators which are
evaluated to make a new value.
โธ Expressions are statements as well.
โธ total_payment = monthly_rate * 12
assignment statement
6. Variable
โธ Variable are used in programming language to hold value.
โธ The variableโs value is actually the Information associated with that variable
name.
โธ variable = expression or single value
โธ age = 0
โธ age = 11 * 3
โธ payment = h_rate * 100
36age Memory
Location
variable
name
7. Naming Convention
โธ Compare these two statements:
โธ S1
โธ S2
1. c = a * b
1. weekly_pay = hours_worked * hourly_pay_rate
Best Practice
8. โธ Arithmetic operators are used to execute arithmetic operations
Arithmetic Operators
Operator Operator Name Example
+ Additional 3 + 5
- Subtraction 3 - 5
* Multiplication 3 * 5
/ Division 7 / 3
% Modulus 7 % 3
** Exponent 2 ** 10
// Floor division 7 // 3
result is 2
9. โธ Arithmetic Assignment operators are used for assigning the values
generated after evaluating the right operand to the left operand
Assignment Operators
Operator Operator Name Example
.= Assignment age = 21
+= Additional Assignment age +=2
-= Subtraction Assignment age -=2
*= Multiplication Assignment age *=age
/= Division Assignment result /=2
**= Exponential Assignment k **= 10
//= Floor Division Assignment result //=2
%= Reminder Assignment result %=2
Variable must be initialized before used
10. โธ Arithmetic Assignment operators are used for assigning the values
generated after evaluating the right operand to the left operand
Comparison Operators
Operator Operator Name Example
.== Equal to age == 21
!= Not Equal to age != -1
> Greater Than age > 18
< Lesser Than age < 35
>= Greater Than or Equal to age >= 30
<= Lesser Than or Equal to age <= 29
11. โธ The logical operators are used for comparing or negating the logical
values of their operands and to return results logical value
Logical Operators
Operator Operator Name Example
and Logical AND age > 18 and sex==male
or Logical OR age > 18 or sex==male
not Logical NOT not age > 12
12. Precedence
โธ Operator precedence determines the way in which operators are
parsed with respect to each other.
Operator Operator Name
() Parantheses
** Exponent
+x,-x Unary Plus, Unary Minus
*,/,//,% Multiplication, Division, Floor division, Modulus
+,- Addition, Subtraction
!=,==,>=,<=,>,< Comparisons
in, is, not in, is not Identity, Membership operators
not Logical NOT
and Logical AND
or Logical OR
Operators with higher precedence become the operands of operators with lower precedence.
13. Data Types
โธ Strings
โธ className=โPython Programmingโ
โธ Number
โธ age = 22
โธ avg = 12.45
โธ Boolean
โธ male = False
โธ None - The absence of value
โธ validEmail = None
All numbers are True and just 0 is False
17. Indentation & Blocks
โธ In Python, Programs get structured through indentation
In Python indentation is a requirement and not
a matter of style or convention
Block 1
Block 1 - Continue
Block 2
Block 2 - Continue
Block 3
18. Comments
โธ Allow you to write comments and notes and also disable some part of
your code.
โธ Comment Types
โธ Single-line: Hash mark indicate single line comments. Anything follow # is
ignored by the python compiler/interpreter
โธ Multi-line comment using โโโ โฆ โโ'
1. #This is a single line comment
2. โโโ Start of multi-line commenting
3. โฆ
4. โฆ
5. End of multi-line commentingโโโ
3 single
quotation
19. Input/Output
โธ In Python, input() function is used to gather data from the user.
โธ The print() function allows a program to display text onto the console.
1. name = input(โEnter your name:โ)
1. print(fโyour name is {name}โ)
f-string
variable or
expression
20. Formatted String
โธ Formatted String or f-strings were introduced in Python 3.6
โธ f-string is a string literal that is pre๏ฌxed with โfโ.
โธ The string may contain replacement ๏ฌelds.
โธ which are expressions enclosed within curly braces {}
22. Type Conversions
โธ You can explicitly cast or convert a variable from one type to another.
โธ int()
โธ str()
โธ ๏ฌoat()
โธ chr()
age = int(value)
cast to
int