Python Identifiers
Alphabets
1
Identifiers
2
Keywords
3
Comments
agenda 4
Data Types
5
Variables
6
Type Casting
7
Constants
8
Alphabets
Communication vs Programming
• Alphabets – Alphabets
• Words – Identifiers
• Reserve words – Keywords
• Statement – Statement
• Paragraph – Block of statement
• Page – blocks
• Article/Chapter etc – Program
• Book - Software
4
Python Alphabets
• A–Z
• a–z
• 0–9
• Characters like *, +, -, _ etc.
5
Identifiers
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).
• e.g. counter, age, studName, studAddress, marks1
• Python is a case sensitive programming language.
• Manpower and manpower are two different identifiers in Python.
7
Python Naming conventions
• Python Class names start with an uppercase letter. All other
identifiers start with a lowercase letter.
• e.g. Student if it is class, student if it is other than class
• Starting an identifier with a single leading underscore indicates that
the identifier is private identifier.
• Starting an identifier with two leading underscores indicates a
strongly private identifier.
• If the identifier also ends with two trailing underscores, the identifier
is a language-defined special name.
8
Keywords
Python Keywords
• Reserve words
• cannot be used as constant or variable or
any other identifier names.
• All the Python keywords contain lowercase
letters only.
• 33 keywords
10
Comments
Python Comments
• programmer-readable explanation or annotation
• ignored by Python interpreter during compilation & execution
• similar to the comments available in PHP, BASH and Perl
Programming languages.
• Single line comment
• #
• Multiline comment
• ‘’’ or “”” to begin
• ‘’’ or “”” to end
12
Data Types
Python Data Types
• Numeric – int, float, complex • Python is dynamically typed
• Sequence – str, list, tuple, range • meaning you don’t need to declare the type
explicitly
• Set – set, frozenset
• it’s inferred from the value.
• Mapping – dict (dictionary) • i = 25 is numeric
• Boolean - bool • i = “Sachin” is sequence or string
• Binary – bytes, bytearray, memoryview • Integer, float, Boolean and string are
primitive data types
• List, tuple, dictionary and set are non-
primitive data types.
14
Data Types and Memory
• integer – 4 (Generally 28 for small int, 36 for big int)
• Float – 24 (minimum 8)
• Str – 49 bytes + 1 byte per character
• Complex – 32 bytes
• List – 64 bytes + 8 bytes per element
• tuple – 48 bytes + 8 bytes per element
• range – 8 bytes
• Set – 216 bytes and grows dynamically
• Dict – 64 bytes and grows dynamically
• Bool – 28 bytes
15
Variables
Python Variables
• variables are the reserved memory locations used to store values.
• when you create a variable, you reserve some space in the memory.
• Python variables do not need explicit declaration to reserve memory
space
• Based on the data type of a variable, memory space is allocated to it.
• By assigning different data types to Python variables, you can store
integers, decimals or characters in these variables.
• A Python variable is created automatically when you assign a value to it.
• age = 37
• name = “Sachin”
17
Variable Naming conventions
• variable should have a unique name through out the program
• A variable name must start with a letter or the underscore
character
• A variable name cannot start with a number or any special
character like $, (, * % etc.
• A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Python variable names are case-sensitive which means Name and
NAME are two different variables in Python.
• Python reserved keywords cannot be used naming the variable.
18
Variable Naming conventions
• A variable name should be meaningful like color, age, name etc.
• It can include multiple words like studName
• In case of multiple words
• Camel case − First letter is a lowercase, but first letter of each
subsequent word is in uppercase. For example: kmPerHour,
pricePerLitre
• Pascal case − First letter of each word is in uppercase. For example:
KmPerHour, PricePerLitre
• Snake case − Use single underscore (_) character to separate words.
For example: km_per_hour, price_per_litre
19
Variable Type
• type() is built-in-function to get the data type of variable
20
Type casting
Casting Type
• The type of the data can be decided using type casting
• The desired data type can be used during value assignment
• Two types
• Implicit
• Explicit
22
Implicit Typecasting
• compiler/interpreter automatically converts object of one type into
other.
• E.g. integer can be automatically casted to float in the expression if
other variable is of float type
• Python – Strongly typed language – doesn’t allow automatic type
conversion between unrelated data types. E.g. String can’t be
converted to integers automatically.
• Bool to int - True is 1 False is 0
23
Explicit Typecasting
• Implicit typecasting is restricted to integer, float and bool
• Programmer can use explicit typecasting to convert one type to
other
• if source type and target type is known, specific conversion function
from library can be used
• e,g, str, int, float
24
Type casting functions
• int() – convert argument to int
• long() – convert argument to long
• float() – convert argument to float
• complex() – creates a complex number
• str() – converts object to string representation
• repr() – convert object x to expression string
• eval() – evaluates the string and returns an object
• tuple() – converts x to tuple
25
Type casting functions
• list() – converts x to list
• set() – converts x to set
• dict() – creates a dictionary, argument must be key-value tuples
• frozenset() – converts x to frozen set
• chr() – converts integer to character
• unichr() – converts integer to Unicode character. Deprecated in 3.0
• ord() – converts single character to its integer value.
• hex() - converts integer to hexadecimal string
• oct() – converts integer to octal string
26
Constants
Python Constant
• Python doesn't have any formally defined constants,
• However, you can indicate a variable to be treated as a constant by
using all-caps names with underscores.
• e.g. _PI, PI_VALUE etc.
• This naming convention is called as screaming snake case
28
References
• [Link]
• [Link]
• [Link]
29
DR. KAULWAR BHAGWAT
Thank you bhagwat_kaulwar@[Link]
Dr. Bhagwat Kaulwar | LinkedIn