Python Basic I Unit
Python Basic I Unit
a = "1,
2,
3,
4,"
# brackets("()")
b = (1,
2,
3,4)
# braces("{}")
c = {1,2,
3,
4}
# square brackets("[]")
d = [1,
2,
3,
4]
# semi-colon(";")
print(a);print(b);
print(c);
print(d);
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.
3) >>> 'ab' * 4
'abababab'
Escaping
There’s another way around this problem, called escaping. You can escape a special
character, like a quote, with a backwards slash:
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:
print(my_string)
3) Example 3: Using \
print(my_string)
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
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
Comparison Operators
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
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.