Chapter 3+4
Chapter 3+4
Ans 4/(3*(r+34)))-(9*(a+b*c))+(3+d*(2+a))/(a+b*d)
Ans To extract only the whole seconds from the returned value,
use the int() function to truncate the fractional part:
Example;
Import time
seconds = int(time.time())
Example;
Function Converts To
int(x) Integer
float(x) Float
str(x) String
bool(x) Boolean
list(x) List
tuple(x) Tuple
set(x) Set
Examples:
Code:
num = 5.6
print(int(num)) # Output: 5
Code:
num = 10
Code:
num = "3.14"
my_list = [1, 2, 3]
Example;
OR
Syntax; input().split(separator)
Example;
result = text.split()
print(result)
Output:
Parameters
1. sep
o A string used to separate multiple objects.
o Default is a single space ' '.
o Example: print("Hello", "World", sep='-') will print Hello-
World.
2. end
o A string added at the end of the printed output.
o Default is a newline \n, which moves the cursor to the
next line.
o Example: print("Hello", end='!') will print Hello!.
3. file
o Specifies where to send the output.
o Default is sys.stdout, which means the output will go to
the console.
o Example: You can write to a file using
file=open('output.txt', 'w').
4. flush
o A boolean that specifies whether to forcibly flush the
output buffer.
o Default is False.
o Example: print("Loading...", flush=True) ensures the
output is immediately visible.
Built-in Libraries
Python has a standard library with essential tools, like:
Third-Party Libraries
These are created by the Python community and installed
using tools like pip. Popular examples include:
Custom Libraries
Developers can create their own libraries to organize
reusable code for specific projects.
Example;
One-Way if Statement
Syntax;
if condition:
# code to execute
Example;
age = 18
Syntax;
if condition:
else:
Example;
num = 5
if num % 2 == 0:
print("Even")
else:
print("Odd")
Syntax;
if condition1:
elif condition2:
Example;
score = 85
print("Grade: A")
print("Grade: B")
else:
print("Grade: C")
Nested if Statements
Syntax;
if condition1:
if condition2:
Example;
num = 15
if num % 5 == 0:
print("Number is greater than 10 and
divisible” “by 5")
Syntax;
Example;
num = 8
print(result)
Ans Pascal case, camel case, and snake case are different ways
of writing compound words or identifiers, especially in
programming, to make them readable and maintainable.
Here’s what each one means:
Pascal Case:
Example;
MyVariableName = 42
Camel Case:
Similar to Pascal case, but the first letter of the first word is
lowercase, and the first letter of each subsequent word is
capitalized.
Example;
myVariableName = 42
Snake Case:
Example;
my_variable_name = 42
import random
i = random.randint(0, 1)