Python Unit1 QB
Python Unit1 QB
in :
Returns True if a sequence with the specified value is present in the
object
Example:
x = ["apple", "banana"]
print("banana" in x)
output:
True
not in:
Returns True if a sequence with the specified value is not present in the
object
Example:
x = ["apple", "banana"]
print("pineapple" not in x)
output:
True
Example:
if 10 > 5:
print("This is true!")
print("I am tab indentation")
Output:
This is true!
I am tab indentation
I have no indentation
ii) Variables:
Variables are containers for storing data values.
Python variables are the reserved memory locations used to store values
with in a Python Program. This means that when you create a variable
you reserve some space in the memory.
Example:
x=5
y = "John"
print(x)
print(y)
Output:
5
John
Example:
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Output:
a and b are equal
Example:
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x
print(x is z)
print(x is y)
print(x == y)
Output:
True
False
True
ii)
is not : Returns true if both variables are not the same object
Example:
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x
print(x is not z)
print(x is not y)
print(x != y)
Output:
False
True
False
ii) Identifiers:
Identifiers in Python
Identifier is a user-defined name given to a variable, function, class, module, etc.
The identifier is a combination of character digits and an underscore. They are
case-sensitive i.e., ‘num’ and ‘Num’ and ‘NUM’ are three different identifiers in
python. It is a good programming practice to give meaningful names to identifiers
to make the code understandable.
iv) Comments: Comments in Python are the lines in the code that are ignored by
the interpreter during the execution of the program.
v) Keywords: Keywords in Python are reserved words that have special meanings
and serve specific purposes in the language syntax. Python keywords cannot be
used as the names of variables, functions, and classes or any other identifier.
3. Easy to Read
As you will see, learning Python is quite simple. As was already established,
Python’s syntax is really straightforward. The code block is defined by the
indentations rather than by semicolons or brackets.
4. Object-Oriented Language
One of the key features of Python is Object-Oriented programming. Python
supports object-oriented language and concepts of classes, object encapsulation,
etc.
6. High-Level Language
Python is a high-level language. When we write programs in Python, we do not
need to remember the system architecture, nor do we need to manage the
memory.
8. Easy to Debug
Excellent information for mistake tracing. You will be able to quickly identify
and correct the majority of your program’s issues once you understand how
to interpret Python’s error traces. Simply by glancing at the code, you can
determine what it is designed to perform.
9. Python is a Portable language
Python language is also a portable language. For example, if we have Python
code for Windows and if we want to run this code on other platforms such
as Linux, Unix, and Mac then we do not need to change it, we can run this code
on any platform.
Example:
# Using break to exit the loop
for i in range(10):
if i == 5:
break
print(i)
output
0
1
2
3
4
continue Statement:
The continue statement skips the current iteration and proceeds to the next
iteration of the loop.
Example:
# Using continue to skip an iteration
for i in range(10):
if i % 2 == 0:
continue
print(i)
Output:
1
3
5
7
9
pass Statement:
The pass statement is a null operation; it does nothing when executed. It’s useful
as a placeholder for code that you plan to write in the future.
Example:
# Using pass as a placeholder
for i in range(5):
if i == 3:
pass
print(i)
output:
0
1
2
3
4
Q.10 Explain Following Terms i) input() ii) print()
Ans: i) input():Python input() function is used to take user input. By default, it
returns the user input in form of a string.
Example:
name = input("Enter your name: ")
print("Hello,", name, "! Welcome!")
Output:
Enter your name: GeeksforGeeks
Hello, GeeksforGeeks ! Welcome!
ii)Print():
The print() function prints the specified message to the screen, or other standard
output device.
The message can be a string, or any other object, the object will be converted into a
string before written to the screen.
Example:
print("Hello", "how are you?")
output:
Hello , how are you?
Python is derived from many other languages, including ABC, Modula-3, C, C++,
Algol-68, SmallTalk, and Unix shell and other scripting languages. Guido van
Rossum wanted Python to be a high-level language that was powerful yet readable
and easy to use.
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Example:
if 10 > 5:
print("Program ended")
output:
10 greater than 5
Program ended
Example:
x=3
if x == 4:
print("Yes")
else:
print("No")
output:
No
Nested if Statement
if statement can also be checked inside other if statement. This conditional
statement is called a nested if statement. This means that inner if condition will
be checked only if outer if condition is true and by this, we can see multiple
conditions to be satisfied.
Example:
num = 10
if num > 5:
output:
Bigger than 5
Between 5 and 15
Example:
letter = "A"
if letter == "B":
print("letter is B")
print("letter is A")
else:
Output:
letter is A
Example:
cnt = 0
cnt = cnt + 1
print("Hello Geek")
Output:
Hello Geek
Hello Geek
Hello Geek
Example:
n=4
print(i)
Output:
0
1
2
3
Example:
for j in range(i):
print()
Output:
1
22
333
4444