DEV Community

Vasanthakumari N
Vasanthakumari N

Posted on Edited on

Python Day 2: Identifiers, Keywords, Data Types & Memory Basics

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
Enter fullscreen mode Exit fullscreen mode

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

  1. We can use uppercase and lowercase alphabets (A-Z, a-z).
  2. We can use digits (0-9), but an identifier cannot start with a digit.
  3. We can use underscores (_).
  4. Spaces are not allowed.
  5. Python keywords cannot be used as identifiers.
  6. 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))
Enter fullscreen mode Exit fullscreen mode

The keyword module provides:

  • iskeyword(): checks if a string is a keyword.
  • kwlist: returns the list of all keywords.

Rules for Keywords in Python

  1. Python keywords cannot be used as identifiers.
  2. 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
Enter fullscreen mode Exit fullscreen mode

2. Float

Float represents numbers with decimal points.

Examples:

price = 99.50
percentage = 85.5
temperature = -1.5
Enter fullscreen mode Exit fullscreen mode

3. Complex

Complex numbers contain real and imaginary parts.

Example:

number = 3 + 4j

print(number)

Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

5. Boolean (bool)

Boolean represents two values:

  • True
  • False

Example:

is_student = True
is_completed = False
Enter fullscreen mode Exit fullscreen mode

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'))
Enter fullscreen mode Exit fullscreen mode
Output:

65
97
Enter fullscreen mode Exit fullscreen mode

We can convert an integer to its corresponding character using chr().

print(chr(65))
Enter fullscreen mode Exit fullscreen mode
Output:

A
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output:

0b1000001
Enter fullscreen mode Exit fullscreen mode

We can also convert a character into its binary representation:

alphabet = 'a'

print(bin(ord(alphabet)))
Enter fullscreen mode Exit fullscreen mode
Output:

0b1100001
Enter fullscreen mode Exit fullscreen mode

Here:

ord() converts the character into its numeric code.

bin() converts that number into binary.

References

Top comments (0)