0% found this document useful (0 votes)
4 views

Python Basic I Unit

The document explains how to create multi-line statements in Python using various entities like braces, parentheses, and continuation characters. It also covers the basics of strings, including creation, escaping special characters, and multiline strings. Additionally, it discusses Boolean types, operators, and conditional statements for controlling program flow.

Uploaded by

MyDream 4U
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)
4 views

Python Basic I Unit

The document explains how to create multi-line statements in Python using various entities like braces, parentheses, and continuation characters. It also covers the basics of strings, including creation, escaping special characters, and multiline strings. Additionally, it discusses Boolean types, operators, and conditional statements for controlling program flow.

Uploaded by

MyDream 4U
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/ 9

Multi-Line Statements in Python

When it comes to extending a Python statement into valid multi-line statements, we


can make use of the following entities:
1. Using Braces -> {}
2. Using parentheses -> ()
3. Using Square brackets -> []
4. Using Semi-colon -> ;
5. Using continuation character slash ->
Take a look at the example below for reference. We have extended Python single-
line statements to multiple lines using the above entities.
Example
# Breaks the statement using

# line continuation character("")

a = "1,

2,

3,

4,"

# Breaks the statement using

# brackets("()")

b = (1,

2,

3,4)

# Breaks the statement using

# braces("{}")

c = {1,2,

3,
4}

# Breaks the statement using

# square brackets("[]")

d = [1,

2,

3,

4]

# Breaks the statement using

# semi-colon(";")

print(a);print(b);

print(c);

print(d);

PYTHON STRING: WORKING WITH TEXT

String
A string is a sequence of characters
In even simpler terms, a string is a piece of text. Strings are not just a Python thing.
It’s a well-known term in computer science and means the same thing in most other
languages. Now that we know a string, we’ll look at how to create one.

Strings in Python are surrounded by either single or double quotation marks.

'Hello' is the same as "Hello".

HOW TO CREATE A STRING


A Python string needs quotes around it for it to be recognised as such, like this:

1) >>> 'Hello, World'


'Hello, World'

2) >>> 'a' + 'b'


'ab'

3) >>> 'ab' * 4
'abababab'

4) >>> 'a' - 'b'


Traceback (most recent call last):
File "<stdin>", line 1, in <module>
5) >>> "a" + "b"
'ab'

6) >>> mystring = "Its a string, with a single quote!"


‘‘Its a string, with a single quote!’’

7) >>> mystring = 'It's a string, with a single quote!'


File "<stdin>", line 1
mystring = 'It's a string, with a single quote!'
^
SyntaxError: invalid syntax

Escaping
There’s another way around this problem, called escaping. You can escape a special
character, like a quote, with a backwards slash:

USING SPECIAL CHARACTERS IN STRINGS

Escaping sequences are used to define a string containing special characters in


various forms.
8) >>> mystring = 'It\'s an escaped quote!'
"It's an escaped quote!"

9) >>> mystring = "Im a so-called \"script kiddie\""


“Im a so-called "script kiddie"'

10) >>>print("\#{'}${\"}@/")
\#{'}${"}@/
11) >>>print("\#{'}${\"}@/")
\#{'}${"}@/

12) >>>print('\#{'"'"'}${"}@/')
\#{'}${"}@/

13) >>>print(r'''\#{'}${"}@/''')
\#{'}${"}@/

Multiline strings
Python also has syntax for creating multiline strings using triple quotes. By this, I
mean three double quotes or three single quotes; both work, but I’ll demonstrate
with double quotes:

1) Example 1: Using triple quotes (Using “ or ‘)

my_string = '''The only way to


learn to program is
by writing code.'''
print(my_string)

The only way to


learn to program is
by writing code.

2) Example 2: Using parentheses and single/double quotes


my_string = ("The only way to \n"
"learn to program is \n"
"by writing code.")

print(my_string)

The only way to


learn to program is
by writing code.

3) Example 3: Using \

my_string = "The only way to \n" \


"learn to program is \n" \
"by writing code."

print(my_string)

The only way to


learn to program is
by writing code.

A BOOLEAN TYPE IN PYTHON

A Boolean type in Python represents truth values and can only be True or False.
It is a fundamental data type used in logical operations and conditional statements.
Boolean values are often the result of comparisons or evaluations.
Any value in Python can be evaluated for its truthiness using the bool() function.
Most values are considered True, except for:
• False
• None
• Zero of any numeric type (e.g., 0, 0.0)

Example-1:
print(10 > 9) True
print(10 == 9) False
print(10 < 9) False

Example-2:
a = 200
b = 33

if b > a:
print("b is greater than a")
else:
print("b is not greater than a")

OUTPUT
b is not greater than a

Evaluate Values and Variables


The bool() function allows you to evaluate any value, and give you True or False in
return

Syntax of bool() function

bool([x])

Parameters
• x: represents the value that we want to convert to a Boolean. If no
argument is provided, bool() returns False by default.

Example-3:
print(bool("Hello")) OUTPUT TRUE
print(bool(15)) OUTPUT TRUE

Example-4:
x = "Hello"
y = 15
print(bool(x))
print(bool(y))

OUTPUT
TRUE

REMEMBERING THE RESULTS OF A BOOLEAN EXPRESSION

What is an example of a Boolean operator?


Boolean operators are used to execute Boolean expressions. Boolean operators
compare the conditional expressions and return a Boolean value. The most common
Boolean operators are: AND, OR and NOT operators.
- (condition1 AND condition2)
AND operator - results true if both the conditions are true
- (condition1 OR condition2)
OR operator - results true if any one of the conditions is true
- NOT (condition)
NOT operator - returns true when the condition is false.

Comparison Operators

These operators compare values and return a boolean result:


• ==: Equal to
• !=: Not equal to
• >: Greater than
• <: Less than
• >=: Greater than or equal to
• <=: Less than or equal to

Example:
1. (x>1) && (x<5)
- returns true if both the conditions are true, i.e if the value of 'x' is between 1
and 5.

2. (x%x) || (x%1)
- returns true if any one condition is true, i.e: either 'x' is divisible by itself OR if 'x'
is divisible by 1.
3. !(x==0)
- returns true if the value of 'x' is other than 0. If the value of 'x' is 0 then it returns
false.

x=5
y = 10

print(x < y and y > 0) # Output: True, both conditions are true
print(x > y or x < 10) # Output: True, at least one condition is true
print(not x == y) # Output: True, x is not equal to y

CHOOSING STATEMENTS TO EXECUTE


In Python, choosing which statements to execute is primarily achieved through
conditional statements. These statements allow the program to make decisions
based on certain conditions. The main conditional statements in Python are if, elif,
and else.

if statement
The if statement is the most basic form of conditional execution. It evaluates a
condition, and if it is true, it executes a code block.

x = 10
if x > 5:
print("x is greater than 5")

elif statement
The elif statement (short for "else if") allows you to check multiple conditions in
sequence. If the if condition is false, the elif conditions are evaluated one by one. The
first elif condition that evaluates to true will have its corresponding code block
executed.
x=3
if x > 5:
print("x is greater than 5")
elif x < 5:
print("x is less than 5")

else statement
The else statement provides a default block of code to execute if none of the
preceding if or elif conditions are true.
x=5
if x > 5:
print("x is greater than 5")
elif x < 5:
print("x is less than 5")
else:
print("x is equal to 5")

Nested if statements
You can also nest if statements within other if or elif blocks to create more complex
decision-making structures.

x = 10
y=5
if x > 5:
if y < 10:
print("x is greater than 5 and y is less than 10")

Logical operators
Conditional statements can use logical operators (and, or, not) to combine multiple
conditions.

x=7
if x > 5 and x < 10:
print("x is between 5 and 10")

These conditional statements provide the fundamental tools for controlling the flow
of execution in Python programs, enabling them to respond dynamically to different
situations and inputs.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy