Python
Statement and Escape
Sequence
MicroTech
Create Workspace
1. Go to C drive->create a folder-> write
folder name “python_workspace”
2. Open visual studio
3. Click on file
4. Click on open folder
5. Select python_workspace
Create Project
1. After opening workspace
click on new folder and give
the name of your project
“class_1”
Create Python file with py extension
1. After creating a project, select the
Project and click on new file
2. Write the file name with extension py
Like in the picture, class_1 is the project
name and [Link] is the file
name.
3. Now write code in file
First Python Program
● In, First python program,
we just write a printing statement,
● To print something we need to write a function
which is print function,
● In function we write a statement in single quote or double quote or triple quote
● We can write anything that we want to print,
● Here we write ‘Hello world’ in [Link] line is a statement.
● print("Hello world")
● In Python 2.x, you can use the print statement to display output. This has been changed to a function in
Python 3.x.
Example of print function
print("Hello world")
print("welcome to python world")
print('Learn Programming with fun')
print('''Python is
easy to learn
''')
Output:
Hello world
welcome to python world
Learn Programming with fun
Python is
easy to learn
Which quote do we use?
● As we say before,
● In Python, you can use different types of quotes (single quotes,
double quotes, or triple quotes) to define strings,
● and the choice of which type to use often depends on the
specific requirements of your string and coding style.
● Here are some guidelines on when to use each type of quote
Single Quotes (' ') and Double Quotes (" "):
Consistency:
The most important guideline is to be consistent within your codebase.
Choose one style (single or double quotes) and stick with it throughout your
code to maintain readability.
Interchangeability:
• Single quotes and double quotes are interchangeable for defining
strings.
• If the string you're making has one type of quote character inside it, you
can choose the other type of quote for creating the string.
• This can make your code easier to write because you won't need to use
special characters to tell Python to treat a quote as part of the string.
Example:
print('She said, "Hello!"')
print("He said, 'Hi!'")
Output:
She said, "Hello!"
He said, 'Hi!'
Triple Quotes (''' ''' or """ """):
Multiline Strings: Triple-quoted strings are particularly useful
when you need to define multiline strings. They can span across
multiple lines without using escape characters:
print('''Python is
easy to learn
''')
Output:
Python is
easy to learn
Statement
In Python, a statement is a line of code that performs a specific action
or task.
It is a fundamental building block of a Python program.
Python statements can include assignments, function calls, control
structures (like if statements and loops), and more.
Each statement typically carries out a specific operation, and they are
executed sequentially, one after the other, in the order they appear in
the code
Type of Statement
• Assignment Statements: These statements are used to assign values to
variables.
• x = 10
• name = "Alice“
• Expression Statements: These are statements that contain expressions. An
expression is a combination of values and operators that, when evaluated,
results in a single value.
• y=x+5
• Print Statements (in Python 2.x): In Python 2.x, you can use the print
statement to display output. This has been changed to a function in Python
3.x. For example (Python 2.x):
• print("Hello, World!")
Type of Statement
• Conditional Statements: These statements are used to make decisions in your
code. The most common conditional statement is the if statement, which
allows you to execute different blocks of code based on a condition.
• if x > 5:
• print("x is greater than 5")
• else:
• print("x is not greater than 5")
• Loop Statements: These statements allow you to repeat a block of code
multiple times. Common loop statements include for and while loops.
• for i in range(5):
• Print(i)
Type of Statement
• Function Calls: Calling a function is also a type of statement. When you call a
function, you're instructing Python to execute a block of code defined
elsewhere in your program.
• result = calculate_sum(5, 3)
• Import Statements: These statements are used to bring external modules
and libraries into your Python program:
• import math
• Pass Statement: The pass statement is a placeholder statement that does
nothing. It's often used when a statement is syntactically required but you
don't want to execute any code:
• if condition:
• pass
Check Version of python
Here, 1st line is a statement , In 2nd line version is a variable we
storing python version inside that, which is a [Link], = sign is
a assigning operator, So, in this line we write a function and a
[Link], in 3rd line we write a function at first which is
print, in print function we write a statement ‘’This is a Python
version, and we called a function format(version)
Indenting:
Indenting is matter in Python. Python uses indentation to indicate a block of code. Python will give you an error if
you skip the indentation. The number of spaces is up to you as a programmer, but it has to be at least one.
8 a=15
9 if a%2==0:
10 print(a," is even Number")#here must give tab or space
11 else:
12 print(a," is odd Number")
Output:
print(a," is odd Number")
^
IndentationError: expected an indented block after 'else' statement on line 11
Block and scope
Block is also important in Python like other language. In this below code,line 4 and 5 is inside if
block, means if in line 3, if condition is true then line 4 and line 5 is will run, they are in if
[Link] as, line 6 is not in same indenting as line 4, and 5 so, line 5 is an independent
[Link] doesn’t depend on if statement.
1 x=37
2 y=22
3 if x>y:
4 z=100
5 print('x greater than y 6{}'.format(z))
6 print('done{}'.format(z))
Output:
x greater than y 100
done100
Comment
● Comments starts with a #, and Python will ignore that line:
#This is a comment
print("Hello, World!")
Multiline Comments:
Though python has no syntax for multiline comments, if you want to add comments, you
can use multiline strings (triple quotes) even if you don't assign them to a variable. Python
will just ignore them.
""" Output:
This is a comment Hello,World!
written in
more than one line
"""
print("Hello, World!")
Escape sequence
In Python, escape sequences are used to represent special characters within strings. These special characters are not directly printable but have
specific meanings. You typically escape them by using a backslash () followed by a specific character or sequence of characters to represent these
special characters. Here are some common escape sequences in Python:
\n: Newline - Represents a line break or a new line.
\t: Tab - Represents a horizontal tab or indentation.
\\: Backslash - Represents a literal backslash.
\': Single Quote - Represents a single quotation mark within a single-quoted string.
\": Double Quote - Represents a double quotation mark within a double-quoted string.
\b: Backspace - Represents a backspace character.
\r: Carriage Return - Represents a carriage return character.
\f: Formfeed - Represents a form feed character.
\v: Vertical Tab - Represents a vertical tab character.
Escape sequence
# Newline and Tab
print("Hello,\nWorld!")
print("This is some text with a\ttab.")
# Backslash and Quotes
print("This is a single backslash: \\")
print("He said, \"Hello, World!\"")
# Backspace, Carriage Return, Formfeed, and Vertical Tab
print("Hello\bGoodbye") # Backspace will erase 'o'
print("Hello\rWorld") # Carriage return moves the cursor to the beginning of the line
print("Hello\fWorld") # Formfeed is a page break character (may not have a visible effect)
print("Hello\vWorld") # Vertical tab (may not have a visible effect)
●
Hello,
World!
This is some text with a tab.
This is a single backslash: \
He said, "Hello, World!"
HellGoodbye
World
Hello World
Hello World
Thank You