Docsity

Complete Python Syntax, Keywords, Variables & Data Types Notes", Lecture notes of Semantics of Programming Languages

Partial preview of the text

Download Complete Python Syntax, Keywords, Variables & Data Types Notes" and more Lecture notes Semantics of Programming Languages in PDF only on Docsity!

Python Syntax,

Keywords, Variables,

Data Types

Presented by

Mohd Shuaib

Assistant Professor

Python Syntax

  • Statement ends with the token NEWLINE character ( i.e. carriage

return).

  • It means each line is a statement in python Script.

Example: 4 statements in 4 separate lines

print('SNo: ', 1)

print('First Name: ', 'Aditya')

print('Last Name: ', 'Joshi')

print('Age: ', 19)

the semicolon ; is used to separate multiple statements in a

single line.

Example:

print('Hello');print('BCA class!');print('how are you’)

Expressions in parentheses (), square brackets [ ], or curly

braces { } can be spread over multiple lines without using .

Example:

list = [10, 20, 30, 40,

90, 100, 110, 120]

Indentation in Python

Leading space or tab at the beginning of the line is considered

as indentation level of the line, which is used to determine the

group of statements. Statements with the same level of

indentation considered as a group or block.

For example, functions, classes, or loops in Python contains a

block of statements to be executed.

C# or Java use curly braces { } to denote a block of code.

Python uses indentation (a space or a tab) to denote a block of

statements.

Example: demonstration of if else blocks

if 10 > 5: # 1st block starts

print("10 is greater than 5") # 1st block

print("Now checking 20 > 10") # 1st block

if 20 > 10: # 1st block

print("20 is greater than 10") # inner block

else: # 2nd block starts

print("10 is less than 5") # 2nd block

print("This will never print") # 2nd block

Function contains a block with 2 statements

def welcomemsg(msg):

print("Hello ", msg)

print("Welcome to Python class BCA Stuents")

You may use either interactive shell or IDLE or any GUI based

IDE

Error will be

IndentationError: expected an indented block if you not use

indentation or IndentationError: unexpected indent

Multiline comments

  • There is no provision for making multi line comments but you can make

multi line string

Python multi-line comment is a piece of text enclosed in a delimiter (“””)

on each end of the comment. Again there should be no white space

between delimiters (“””). Single or double quotes both may be used

Write Python code here

""" Multi-line comment used

print("Python Comments") """

print("Hello Class")

  • However, if these multiline comments are placed directly after a

function or class signature, then these turn into docstrings.

  • Docstring is an in-built feature of Python, which is used to

associate documentation that has been written with Python

modules, functions, classes and methods. It is added right below

the functions, modules, or classes to describe what they do. In

Python, the docstring is then made available via the doc

attribute.

def add(a, b):

"""adds the value of a and b"""

return a+b

Print the docstring of add function

print(add.doc)

print(add(2, 10))

Identifiers in Python are case sensitive, which means variables

named age and Age are different.

Class names should use the TitleCase convention. It should

begin with an uppercase alphabet letter e.g. MyClass, Student,

Employee.

Function names should be in lowercase. Multiple words should

be separated by underscores, e.g. add(num),

calculate_tax(amount).

Variable names in the function should be in lowercase e.g., x,

num, salary.

Module and package names should be in lowercase e.g.,

mymodule, tax_calculation. Use underscores to improve

readability.

Constant variable names should be in uppercase e.g., RATE,

TAX_RATE.

Display Output

  • The print() serves as an output statement in Python. It echoes the value

of any Python expression on the Python shell.

  • Multiple values can be displayed by the single print() function separated

by comma.

The following example displays values of name and age variables using the

single print() function.

name="Aditya"

print(name) # display single variable

age=

print(name, age)# display multiple variables

print("Name:", name, ", Age:",age) # display formatted output

By default, a single space ' ' acts as a separator between values. However,

any other character can be used by providing a sep parameter.

name=input("Enter your name: ")

print(type(name))

age=input("Enter your age: ")

print(type(age))

Keywords

  • These are predefined words having special purpose and cannot be used for other

things

  • Except for the first three (False, None and True), the other keywords are entirely in

lowercase.

Get it by

help("keywords") 35 keywords

Here is a list of the Python keywords. Enter any keyword to get more help.

False class from or

None continue global pass

True def if raise

and del import return

as elif in try

assert else is while

async except lambda with

await finally nonlocal yield

print() is also used to display the value of variable

>>> print(n) #display value

>>> print(n + 2) # add and display the result

Variables in Python are objects. Use the type() function to get

the class name of an object.

>>> type(n)

The type of n is int. An object of int class contains a integer

literal 20

Depending upon the value they are all class

>>> msg='Hello World'

>>> type(msg)

>>> isPythonInterpreted = True

>>> type(isPythonInterpreted)

Python is a dynamically-typed language, which means you

don't need to declare a type of a variable. The type will be

assigned dynamically based on the assigned value.