Unit 3: Advance Python
Question 1: Explain any 5 features and applications of Python.
Answer:
→ 5 features of Python:
• Easy language:
Python is an easy language. It is easy to read, write, learn and understand.
• Interpreted language:
Python is an interpreted language, which means that the programs are passed straight to an interpreter that executes them directly.
• Open source:
Python is an open-source programming language.
• Free of cost:
Python is free to download and use.
• Cross-platform:
Python is compatible with various operating system, like Mac, Windows, Linux, Unix, etc.
→ 5 applications of Python:
• Web development:
Python helps the user in writing server-side code, which helps in managing databases, write backend programming logic, mapping URLs etc.
• Game development:
Python is suitable for building tools for game designers that simplify tasks, such as “level designing” or “dialogue tree creation” etc.
• Machine learning:
Python has all the qualities that a machine learning application requires. Hence it has become one of the most favoured languages of Data Science professionals.
• Scripting:
Scripting is writing small programs to automate simple tasks, like sending automated response emails. Such type of applications can also be written in Python programming language.
• Data analysis:
Python has a unique attribute and is easy-to-use when it comes to quantitative and analytical computing. Its massive libraries are used for data manipulation and very easy to learn even for a beginner data analyst.
Question 2: Explain variable with syntax and example and mention the rules for naming variable in Python.
Answer: Variables in Python can be thought of as containers for storing data values. The value of the data stored in variables, i.e., the value of variables, can change depending upon the program. The variables are created when the values are assigned to them. The syntax for assigning a variable to the variable is: variable_name = variable_value. When writing a variable name that has more than 1 word, make sure that an underscore (_) is put between the gaps of words. Text can also be assigned to the variable by enclosing them in double (" ") or single (' ') quotes. Rules for naming a variable:
Rule 1: The name of a variable must start with a letter or an underscore (_).
Example: _hello, Hello, HELLO etc. are accepted. But variable names like 24X, #name, (Name etc. are not accepted.
Rule 2: The variables names can only have alphanumerical characters and underscores.
Example: hello, h_e_l_l_o, hello123, B_2_2 etc. are accepted. But variable names like AG#E, qwerty@ etc. are not accepted.
Rule 3: The variable names are case sensitive.
Example: The variable name age differs from the variable name AGE.
Rule 4: You can't use a reserved keyword for naming a variable.
Question 3: Explain various operators supported by python with an example.
Answer:
→ Arithmetic Operators:
Arithmetic operators are used in Python for performing mathematical operations on variables and values. Python has 7 arithmetic operators. The following table shows the 7 operators:
Operator |
Name/Meaning |
Example |
Comment |
+ |
Addition |
x + y |
Returns the sum of the values |
- |
Subtraction |
x - y |
Returns the difference of the values |
* |
Multiplication |
x * y |
Returns the product of the values |
/ |
Division |
x / y |
Returns the quotient of the values |
// |
Floor division |
x // y |
Returns the integral quotient of the division |
% |
Remainder |
x % y |
Returns the remainder of the division |
** |
Exponent or raised to power |
x ** y |
Returns the exponential of the values |
Example:
Code |
Result |
print(20 + 10) |
30 |
print(20 - 10) |
10 |
print(20 * 10) |
200 |
print(20 / 10) |
2.0 |
print(20 // 10) |
2 |
print(10 % 20) |
10 |
print(20 ** 2) |
400 |
→ Conditional Operators:
Comparison operators are used to compare values. It returns either True or False according to the condition. Python has 6 conditional operators. The following table shows the 6 operators with example:
Operator |
Comment |
Example |
> |
Greater than - True if left operand is greater than the right |
x > y |
< |
Less than - True if left operand is less than the right |
x < y |
== |
Equal to - True if both operands are equal |
x == y |
!= |
Not equal to - True if operands are not equal |
x != y |
>= |
Greater than or equal to - True if left operand is greater than or equal to the right |
x >= y |
<= |
Less than or equal to - True if left operand is less than or equal to the right |
x <= y |
→ Logical Operators:
Logical operators are the and, or, and not operators. The following table shows the 3 operators with example:
Operator |
Example |
Expression |
Result |
and |
And operator |
True and true |
True |
|
True and false |
False |
|
or |
Or operator |
True or false |
True |
|
False or false |
False |
|
not |
Not operator |
Not false |
True |
|
Not true |
False |
And operator:
Logical and operator returns True if both the operands are True else it returns False.
Or operator:
Logical or operator returns True if either of the operands is True.
Not operator:
Logical not operator work with the single Boolean value. If the Boolean value is true it returns False and vice-versa.
→ Assignment Operators:
Assignment operators are used in Python to assign values to variables.
a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left.
Operator |
Example |
Equivalent to |
= |
x = 5 |
x = 5 |
+= |
x += 5 |
x = x + 5 |
-= |
x -= 5 |
x = x - 5 |
*= |
x *= 5 |
x = x * 5 |
/= |
x /= 5 |
x = x / 5 |
Question 4: Explain input() function with syntax and example.
Answer: The input() is an inbuilt function for getting user’s response. The syntax for input() function is:
input(prompt)
The default data type for the input provided by the user is the string data type.
Example,
print("Enter your name: ")
x = input()
print("Hello " + x)
Question 5: Explain if and else statement with syntax and example.
Answer:
→ if statement:
The body of the if statement is executed only if the corresponding condition evaluates to true. The piece of code inside the if statement is skipped if the condition evaluates to false.
Syntax:
if condition:
body
Example,
x = 5
if x == 5:
print("x = 5")
if x > 7:
print("x is more than 7!")
Result:
x = 5
→ else condition:
The condition in the if statement is first checked. If it evaluates to true, the statements in the if-block will be executed. If the condition evaluates to false, the control moves to the else statement.
The else statement gets executed only if all the preceding conditional statements evaluate to false.
Syntax:
if condition:
body
else:
body
Example,
print("Enter a number {x}: ")
x = input()
if x == 5:
print("x = 5")
else:
print("x is more than or less than or equal to 10!")
Result:
Enter a number {x}:
10
x is more than or less than or equal to 10!
No comments:
Post a Comment