In today's class, I learned about identifiers, reserved words (keywords), Python IDEs, and basic data types.
1. Identifiers in Python
An identifier is a name given to a variable, function, class, or other objects in a Python program.
For Example:
name = "Sanjay"
doubt = False
questions = 5
Here:
- name is an identifier.
- doubt is an identifier.
- questions is an identifier.
The values assigned to them are "Sanjay", False, and 5.
Rules for naming identifiers
- We can use uppercase and lowercase alphabets (A-Z, a-z).
- We can use digits (0-9), but an identifier cannot start with a digit.
- We can use underscores (_).
- Spaces are not allowed.
- Python keywords cannot be used as identifiers.
- Python is case-sensitive.
Reserved Words (Keywords)
Some words in Python have predefined meanings. These words are called keywords.
Ex: while,if,elif,else,for,break,continue,return
List of all the keyword names can be accessed using the below code:
import keyword
print(keyword.kwlist)
print(len(keyword.kwlist))
The keyword module provides:
- iskeyword(): checks if a string is a keyword.
- kwlist: returns the list of all keywords.
Rules for Keywords in Python
- Python keywords cannot be used as identifiers.
- All the keywords in Python should be in lowercase except True and False.
Data types in python
A data type defines the type of value that a variable can hold.
In Python, we commonly work with numbers, text, and Boolean values.
Let's understand the basic data types.
1. Integer (int)
Integer represents whole numbers without decimal points.
Examples:
age = 50
number = 100
negative = -10
2. Float
Float represents numbers with decimal points.
Examples:
price = 99.50
percentage = 85.5
temperature = -1.5
3. Complex
Complex numbers contain real and imaginary parts.
Example:
number = 3 + 4j
print(number)
Python uses j to represent the imaginary part.
4. String (str)
A string represents a sequence of characters.
We can create strings using single quotes or double quotes.
Examples:
name = "Amalraj"
alphabet = 'a'
message = "Welcome to Python"
5. Boolean (bool)
Boolean represents two values:
- True
- False
Example:
is_student = True
is_completed = False
Boolean values are useful when working with conditions.
How Does a Computer Understand Characters?
Computers understand binary data (0s and 1s).
But humans work with letters, numbers, and symbols.
So how does a computer understand a character like A or a?
This is where character encoding comes into the picture.
ASCII
ASCII stands for American Standard Code for Information Interchange.
It assigns numerical codes to characters.
We can check the ASCII value in Python using ord().
print(ord('A'))
print(ord('a'))
Output:
65
97
We can convert an integer to its corresponding character using chr().
print(chr(65))
Output:
A
What is Binary?
Binary is a number system that uses only two digits: 0 and 1
We can convert a decimal number into binary using Python's bin() function.
Example:
print(bin(65))
Output:
0b1000001
We can also convert a character into its binary representation:
alphabet = 'a'
print(bin(ord(alphabet)))
Output:
0b1100001
Here:
ord() converts the character into its numeric code.
bin() converts that number into binary.
Top comments (0)