You are on page 1of 3

Python Programming Tutorial

Day 02
Agenda:
1) print function
2) keywords
3) Identifiers

Print function:
The print() function is used to display text or other data on the console. You can
pass one or more arguments to the print() function, and it will output them
separated by spaces by default.
Here's a basic usage example:
print("Hello, world!")
This would output
Hello, world!
You can also pass multiple arguments to print():
name = "Ramesh"
age = 30
print("Name:", name, "Age:", age)
Output: Name: Ramesh Age: 30
Additionally, you can use formatted strings (f-strings) with print() for more
complex output:
name = "Ramesh"
age = 30
print(f"Name: {name}, Age: {age}")
Name: Ramesh, Age: 30
Comments in Python :
In Python, comments are used to annotate code to make it more readable and
understandable. Comments are ignored by the Python interpreter and are meant
for human readers. There are two ways to write comments in Python:
1. Single-line comments: These comments start with the `#` symbol and
continue until the end of the line. Anything after the `#` symbol on that line is
considered a comment.
# This is a single-line comment

7
print("Hello, world!") # This comment explains what the print statement does

2. Multi-line comments: Python doesn't have a built-in syntax for multi-line


comments like some other languages do, but you can use triple quotes (`'''` or
`"""`) to create multi-line strings, which can serve as multi-line comments.
Since these strings are not assigned to any variable, Python ignores them.
'''
This is a multi-line comment.
It spans multiple lines.
'''
print("Hello, world!")

Keywords in Python:
Keywords in Python are reserved words that have special meanings and cannot
be used as identifiers (such as variable names or function names). They are used
to define the syntax and structure of the Python language. Here's a list of Python
keywords as of Python 3.12:

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if',
'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while',
'with', 'yield']

These keywords are case-sensitive, so `True` and `true` are different entities in
Python. It's important to avoid using keywords as variable names or any other
identifiers in your code to prevent syntax errors or unexpected behavior.
To print keywords in python use below program
Program:
import keyword #keyword is a module in python
#in this keyword module all the keywords are stored
print(keyword.kwlist)
Output:
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if',
'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while',
'with', 'yield']

Identifiers in Python:

8
Identifiers in Python are names used to identify variables, functions, classes,
modules, or other objects in a program. They can consist of letters (both
uppercase and lowercase), digits, and underscores, but they must follow certain
rules:
Rules for Python Identifiers:
1. Valid characters: Identifiers can include uppercase and lowercase letters (A-
Z, a-z), digits (0-9), and underscores (_).
2. Cannot start with a digit: Identifiers cannot start with a digit. They must begin
with a letter (uppercase or lowercase) or an underscore (_).
3. Case-sensitive: Python identifiers are case-sensitive. For example,
`myVariable`, `MyVariable`, and `MYVARIABLE` are three distinct identifiers.
4. Cannot use reserved words: Identifiers cannot be the same as Python
keywords or reserved words (such as `if`, `else`, `for`, `while`, etc.).
5. No special characters: Identifiers cannot contain special characters such as !,
@, #, $, %, etc. They can only contain letters, digits, and underscores.

Here are some examples of valid identifiers:


my_variable
MyVariable
variable_1
variable_2
_var
And here are some examples of invalid identifiers:
1_variable # Cannot start with a digit
my-variable # Contains a special character
if # Same as a reserved word
class # Same as a reserved word

You might also like