0% found this document useful (0 votes)
5 views8 pages

Computer Notes

The document explains the concept of boolean flags in Python, which are boolean variables used to track the state of a program and control flow in conditional statements. It also discusses constants, their importance in programming, and how to declare them in both pseudocode and Python. Additionally, the document covers casting to boolean and how different data types convert to boolean values.

Uploaded by

karvishah137
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)
5 views8 pages

Computer Notes

The document explains the concept of boolean flags in Python, which are boolean variables used to track the state of a program and control flow in conditional statements. It also discusses constants, their importance in programming, and how to declare them in both pseudocode and Python. Additionally, the document covers casting to boolean and how different data types convert to boolean values.

Uploaded by

karvishah137
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

1.

5 Data in text-based programs

Flags:

Flags in Python are boolean variables that help track the state or condition of a program, usually
using `True` or `False` values.
Flags are commonly used in conditional statements (like `if` or `while`) to control the flow of a
program depending on whether certain criteria are met.

What is a Boolean Flag?

A boolean flag is just a variable holding `True` or `False`, typically used to represent whether a
requirement is satisfied, a task is completed, or a condition is met. For example:

- `is_adult = (age >= 18)` sets the flag to `True` if age is 18 or more, otherwise `False`.
- Flags are often named with descriptive identifiers like `is_valid`, `is_found`, or
`has_permission`.

Using Flags in Conditional Statements

- In conditional statements, the value of the flag determines whether a block of code executes.

- Example:
Python

age = int(input("Enter your age"))


is_adult = (age >= 18)
if is_adult:
print("You are an adult")
else:
print("You are not an adult")

Here, `is_adult` is a flag used as the condition for the `if` statement.

Storing Comparison Results as Flags

- The result of a comparison can be stored directly in a boolean flag, as in our example: `age =
(input1 >= 18)`.
- This makes the code cleaner and easier to understand because it avoids extra `if-else` logic
and captures the intent in a single statement.

Why Use Boolean Flags?

- They help make code more readable by expressing program logic in a way that's easy to
follow.
1.5 Data in text-based programs

- Flags are used in loops, conditionals, and other structures to keep track of whether specific
conditions are true or false at any time.

A boolean flag efficiently tracks the answer to a yes/no question or the state of a program,
making the code flexible and logical.

input1=int(input(“Enter your age:”))


if input1>=18:
​ age=True
else:
​ age=False
print(age)

In the above code example, `age = (input1 >= 18)` is a flag expression—it checks if the age is
18 or above, and stores the result (`True` or `False`) in the variable `age`.

The result of a comparison can be stored in a variable, as shown below:

input1=int(input(“Enter your age:”))


age = input1>=18:
print(age)

Please refer to programming task 1.17, where later in the program instead of using the condition
again, flag is being used.
1.5 Data in text-based programs

Some Examples of Flags

1.​ Without using boolean flag:

input1=int(input("Enter your age:"))


if input1>=18:
print("True")
else:
print("False")

Using boolean flag:-

input1=int(input("Enter your age:"))


if input1>=18:
age=True
else:
age=False
print(age)

The conditions statement (all codes from line 2 onwards) can be rewritten as
below:-

input1=int(input("Enter your age:"))


age=(input1>=18)
print(age)

2.​ Without using boolean flag:

input1=int(input("Enter your age:"))


if input1>=18 and input1<=60:
print("Eligible to vote")
else:
print("You're not eligible")

Using boolean flag:-

input1=int(input("Enter your age:"))


age=(input1>=18 and input1<=60)
if age:
print("Eligible to vote")
else:
print("NA")
1.5 Data in text-based programs

Constant:

What is a constant?

If something is constant, it means that it does not change.


In programming, a constant is similar to a variable. A constant has an identifier(name) that
points to a space in memory where we can store a value.

An example of a constant is pi = 3.14.

When do we need constant variables?

1.​ If we store a value in a constant, then it will not be changed accidentally later in a
program. By storing it as a constant, we can use its identifier in our code whenever we
need that value every time.
2.​ If we ever need to change the value to constant stores, we only need to do it once - on
the line of code where we assigned it. To do this we will need to stop the program,
change the code, and run the program again.

However, we do not always need to use constants. Each time we create a constant, we are
taking a space in memory. If we use the value instead, we will use less memory. ​
It only comes to think about constants when we are writing longer programs where memory is
limited.

Identifying constants

Example 1 :

​ INPUT number
​ result = number +10
output result

Example 2 :

​ INPUT firstItemWeight
​ OUTPUT “The cost is”,firstItemWeight * 2.5
INPUT secondItemWeight
OUTPUT “The cost is”,secondItemWeight * 2.5
1.5 Data in text-based programs

Constant in algorithm

A constant is usually declared at the beginning of the program.


Writing a declaration:

-​ A keyword
-​ An identifier
-​ An assignment
-​ The value to be stored

In pseudocode, constat will be written as below:

constant ten = 10

In Python, constat will be written as below:


The uppercase letters in the identifier show it is a constant

TEN = 10

Now, we will re-write the above mentioned example using constant

Example 1 :

​ INPUT number
​ result = number * 10
output result

Pseudocode:

​ Constant ten =10


​ INPUT number
​ result = number * ten
output result

Python:

TEN =10
number=inlt(input(“Enter a number:”))​
1.5 Data in text-based programs

​ result = number * TEN


print(result)

Another example of python code with constant:

MARKS1=80
MARKS2=50
MARKS3=30
grade = int(input("Enter your grade:"))
if grade >= MARKS1:
print("Distinction")
elif grade >= MARKS2:
print("Merit")
elif grade >= MARKS3:
print("Pass")
else:
print("Try again")
1.5 Data in text-based programs

Casting to Boolean:

Examples of casting to string, integer and real:

Casting to String
x=5
x1 = str(x)
print(x1)
print(type(x1))

Casting to Integer
y = "42"
y1 = int(y)
print(y1)
print(type(y1))

Casting to Real (Float)


z = "3.14"
float_z = float(z)
print(float_z)
print(type(float_z))

Below data types we may have already used when casting to python:

1.​ int(“0”)
2.​ str(12)
3.​ real(“10.20”)

The command bool() converts data to a boolean data type.


The boolean values in Python are True and False, and casting is useful for checking the logical
value of an expression, variable, or value.

# Casting None, Empty Sequence, Empty Mapping, and Zero

x = None
print(bool(x)) # Output: False

y = ()
print(bool(y)) # Output: False

z = {}
1.5 Data in text-based programs

print(bool(z)) # Output: False

a=0
print(bool(a)) # Output: False

# Casting Non-Empty String

s = 'Hello'
print(bool(s)) # Output: True

# Casting Integers and Floats

i1 = 1
print(bool(i1)) # Output: True

f1 = -3.14
print(bool(f1)) # Output: True

# Casting Empty String

s2 = ''
print(bool(s2)) # Output: False

Any empty object (like '', [], {}, (), None, 0, 0.0) converts to False.​
Non-empty values or non-zero numbers convert to True.​

Example 1 :

data = input(“Enter a word”)


if bool(data) == False:
​ print(“No data”)
else:
​ print(“Data”)

Example 2 :

user_input = ""
if bool(user_input):
print("we entered something!")
else:
print("Input was empty.")

You might also like