0% found this document useful (0 votes)
1 views5 pages

Python_Unit 1_Python Building Blocks (1)

The document provides an overview of Python's features, building blocks, and control flow statements. It covers identifiers, keywords, indentation, variables, and comments, explaining their significance and usage in Python programming. Additionally, it discusses the rules for creating identifiers and the importance of comments for code clarity.

Uploaded by

KIRUTHIGA G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views5 pages

Python_Unit 1_Python Building Blocks (1)

The document provides an overview of Python's features, building blocks, and control flow statements. It covers identifiers, keywords, indentation, variables, and comments, explaining their significance and usage in Python programming. Additionally, it discusses the rules for creating identifiers and the importance of comments for code clarity.

Uploaded by

KIRUTHIGA G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction and Control Flow statements in Python

Features of Python - interactive, object oriented, interpreted, platform independent. Python


building blocks - identifiers, keywords, indentation, variables, comments. Python data types:
numbers, string, tuples, lists, dictionary. Basic operators: arithmetic, comparison/ relational,
assignment, logical, bitwise, membership, identity operators, Python operator precedence.
Control flow: conditional statements (if, if else, nested if), looping in python (while loop, for
loop, nested loops) loop manipulation using continue, pass, break, else

Python building blocks

Language basic building blocks like identifiers keywords variables their types and commenting
section. This basic contains are used and applied in almost every program. In this section we
will see and discuss this Python building blocks.

Python identifiers

Any name is called as identifier. These names include variables name, functions name, class
name, object name, package name and module name, etc.

Like other programming knowledge, python also has same rules of giving identifier to an
entity: such as,

• They start with letter A-Z or a-z or underscore [ _ ]. Internally, they can have digits
but not to start with.

• Their length is not Limited. But referred to be meaningful.

• They should not contain whitespaces.

• They are case sensitive. That is 'num' and 'NUM' are different.
• They should not be keywords. (it will show error, saying "Syntax error: Invalid
syntax") .

Some example of valid and invalid identifiers :


• num #valid

• wordCount #valid

• account_number #valid

• x_co-ordinate #valid

• _4 #valid, but meaningless

• ErrorNumber4 #valid

• Plot#3 #invalid because of special symbol #


• account number #invalid because of space
• 255 #invalid because starts with digit
• empno. #invalid because of special symbol .

• and #invalid because and is a keyboard

And one more important thing, while using underscore [ _ ] ine identifiers for class members,
be careful.

Because the count and appearance of underscore[ _ ] identifiers for class members make it
behave like access specifier.

• _name means, not allowed to import[ like friendly/ default member in Java]

• __ name means, system name

• __ name means,private member

Python keywords

Keywords are the words whose meaning are already known to the compiler and also called as
reserved words. Python has total 35 keywords.

Python Indentation
Most of the previous programming languages uses pair of curly braces { } to define a logical
block. But Python uses indentation.

A logical block of code (such as, body of a function, loop, class, etc.) starts with indentation
and ends with the first unindented line.

For ease of programming most of the programmers prefers single space or tab. (PyCharm
IDE uses a single Tab, by default). See following example.

def test function():

f=1
for i in range(1,11):
if i % 3== 0:
continue

print(i)

print("out of for loop")

Python also allows writing the code in same line, instead of indenting. See the following
code.

if True:

print("Hello")
#can also be written as:

if True: print("Hello"); a=5

Python Variables

Variables are the reserved memory locations to store a runtime value that has an identifier
(name). It means when we create a variable, it reserves a memory location. In previous
programming languages, we also said that variable is the type of memory block that allows
changing its value during runtime.
Based on the data type of a variable, the interpreter allocates memory and decides what can
be stored in variable's memory. Therefore, by assigning different data types to variables, we
can store integers, decimals or characters in variable's memory.

The best part of Python variable is, It do not need explicit declaration of variables. The
declaration and creation happens automatically when we assign a value to variable. i.e. the
identifier that appears on left side of assignment operator (=) creates memory location.

For example:
num = 45 # will make "integer" assignment to "num"

This will automatically create a memory location named "num" and assigns 45 to it. Remember,
Python don't have any explicit declaration of variable. But based on the value we assign to
variable, it automatically assigns that datatype. In above statement, Python will automatically
make assign "integer" datatype to "num".

• avg= 50.25 # makes "float" assignment to "avg"


• website = "chromecoder" # makes "string type" assignment [=] to "website"
• con=True # makes "boolean" assignment to "con"

Python also allows to assign single value to multiple variables.


• For example: a = b = c = 50.45
Here, an float object is created with the value 50.45, and all three variables are assigned to the
same memory location. One more interesting valid assignment statement is :
• n1, n2, n3 = 10, 20, 30
will follow the assignment sequence and hence assigns 10 to n1, 20 to n2 and 30 to n3. The
same thing is also possible while assigning values of different data types.
For example :
• avg, name, con = 65.55, "Rahul", True

And most important thing is follow the identifier rules that we discussed Above.

Comments in Python

Comments are very important while writing a program. It describes the purpose and
application of written code. So that anyone else, who is updating or understanding the written
code, does not have hard time to figure it out. In Python we have two types of comments.
We know that comment is the section in Python code that will neither be compiled nor
executed. It is only for understanding and testing purpose.
Python provides two types of comments.

1. Single-line comment
2. Multi-line comment

1. Single-line comment is the comment that comments a single line of code and extends up
to the newline character.We have hash (#) symbol as single-line comment. Refer following
examples.

Example 1:

# this is single line comment


print("Hello all")
Example 2:

# this is single line comment


# this will display hello all
print("Hello all")
Example 3:

print("Hello all") #this will display Hello all


Example 4:

if n %2 ==0:
# means, if number is divisible by 2
print("This number is even")
2. Multi-line comments are the comments that extend up to multiple lines. Obviously we can
keep using # for multiple continue lines. Refer following examples.

Example 1:

# this can also be done


# to make a multiline
# comment section
print("Hello all")
But Python typically offers multiple line commenting facilities using triple quotes using
single (''') or double (""").For example, above multi-line comment section can be written as :

Example 2:

'''this can also be done


to make a multiline
comment section'''
print("Hello all")
OR
Example 3:

""" this can also be done


to make a multiline
comment section """
print("Hello all")
The triple quotes double (“””) comments can be used as a doc-string. DocString is a short
documentation string.

We can write a DocString as the first statement of any function or class or module.

For example,

def square( num):


"""this method returns square of parameter passed"""
sq = num * num
return sq

Above prepared DocString can be retrieved anytime by using the system attribute _ _ doc_
_ of the function.

For example, you can obtain above DocString by using following statement.
print(square._doc_) .

You might also like