0% found this document useful (0 votes)
20 views

Python Tute 1

The document provides examples and explanations of Python programming concepts including data types, operators, conditional statements and loops. It tests the reader's understanding with examples and asks them to predict the output. Key concepts covered include strings, lists, tuples, sets, dictionaries, boolean, numeric and complex data types, comparison, arithmetic, logical and membership operators.

Uploaded by

sanjaydhingra811
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)
20 views

Python Tute 1

The document provides examples and explanations of Python programming concepts including data types, operators, conditional statements and loops. It tests the reader's understanding with examples and asks them to predict the output. Key concepts covered include strings, lists, tuples, sets, dictionaries, boolean, numeric and complex data types, comparison, arithmetic, logical and membership operators.

Uploaded by

sanjaydhingra811
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
You are on page 1/ 6

Tutorial 7-8

1. Which of the following declarations are valid?

a. 9lives=9 b. flag=0 c. _9lives=’cat’ d. year2=’Sophomore’ e. _$$=7 f. name = ‘France’ g. 7=age

h. False=choice

2. Initialize age to 7 and display it. Re-initialize age to ‘Dinosaur’ and then display the value.

3. Initialize a to -7 and display the datatype of a.

4. Initialize a to 9999999999999999999999999999999 and display the datatype of a.

5. Initialize a to 3.0 and display the datatype of a.

6. Initialize a to 2+3j and display the datatype of a.

Python print() and input():

1. Display India using print().

2. Display 153 using print().

3. Display the product of 72 and 82 in the following format: 72 * 82 = 5904.

a. Manually format the above statement. b. Use .format()

4. Display Malaysia and Argentina separated by a space, comma and a dollar sign.

5. Ask the user to input a float value and display its type

Comparison Operators (==,<,>,<=,>=)

Operator Example Meaning Result


== a == b Equal to True if the value of a is equal
to the value of b
False otherwise
!= a != b Not equal to True if a is not equal to b
False otherwise
< a<b Less than True if a is less than b
False otherwise
<= a <= b Less than or equal to True if a is less than or equal
to b
False otherwise

[Type here]
Tutorial 7-8

> a>b Greater than True if a is greater than b


False otherwise
>= a >= b Greater than or equal to True if a is greater than or
equal to b
False otherwise

1. What will be the output of the following programs:

a.
a = 10
b = 20
print(a == b)
print(a != b)

b.
a = 30
b = 15
print(a <= b)
print(a >= b)
print(a>b)
print(a<b)
Arithmetic Operators (+,-,/,%,**)

Operator Example Meaning Result


+ (unary) +a Unary Positive it doesn’t really do anything. It
mostly exists for the sake of
completeness
+ (binary) a+b Addition Sum of a and b
- (unary) -a Unary Negation Value equal to a but opposite in
sign
- (binary) a-b Subtraction b subtracted from a
* a*b Multiplication Product of a and b
/ a/b Division Quotient when a is divided by b.
The result always has type float.
% a%b Modulo Remainder when a is divided by b

[Type here]
Tutorial 7-8

// a // b Floor Division (also Quotient when a is divided by b,


called Integer rounded to the next smallest whole
Division) number
** a ** b Exponentiation a raised to the power of b

1. Print the outputs:

a=4
b=3
print(+a)
print(-b)
print(a + b)
print(a - b)
print(a * b)
print(a / b)

2. Predict the outputs:


a=5
b=2
print(a % b)
print(a ** b)
print(10 / 4)
print(10 // 4)
print(10 // -4)
print(-10 // 4)
print(-10 // -4)
Logical Operators (not,or,and)

Operator Example Meaning


not not x True if x is False
False if x is True
(Logically reverses the sense of x)
or x or y True if either x or y is True
False otherwise

[Type here]
Tutorial 7-8

and x and y True if both x and y are True


False otherwise

1.Predict the output

x=5
y = 20
print(x > 10 or y < 8)
print(x > 10 or y > 8)
print(x < 10 or y > 8)

2.Predict the output

x=5
y = 20
print(x > 10 and y < 8)
print(x > 10 and y > 8)
print(x < 10 and y > 8)

3.Predict the output

x=5
y = 20
print(not x > 10 )
print(not x < 10)
print(not y > 8)
print(not y < 8)
Data Types (set,list,numbers,tuples)

Data Type Meaning


Booleans Boolean in Python can have two values – True or
False
Numbers The numbers in Python are classified using the
following keywords: int, float, and complex.
Strings A sequence of one or more characters enclosed
within either single quotes ‘ or double quotes ” is
considered as String in Python. Any letter, a number
or a symbol could be a part of the sting.
[Type here]
Tutorial 7-8

Lists Lists in Python can be declared by placing elements


inside square brackets separated by commas.
Tuples A tuple is a heterogeneous collection of Python
objects, using enclosing parentheses ( ) having its
elements separated by commas inside.
Sets A set is an unordered collection of unique and
immutable objects. Its definition starts with
enclosing braces { } having its items separated by
commas inside.
Dictionaries Python syntax for creating dictionaries use braces { }
where each item appears as a pair of keys and values.

1. Predict the outputs:

str = 'Learn Python'

print(len(str))

print(len(str) == 12)

print(len(str) != 12)

2. Predict the outputs:


num = 2
print(type(num))
num = 3.0
print(type(num))
num = 3+5j
print(type(num))

3. Predict the outputs:

my_string = 'Hello'
print(my_string)

my_string = "Hello"
print(my_string)

[Type here]
Tutorial 7-8

my_string = '''Hello'''
print(my_string)

my_string = """Hello, welcome to


the world of Python"""
print(my_string)

4. Predict the outputs:


assorted_list = [True, False, 1, 1.1, 1+2j, "Learn", "b","Python"]
first_element = assorted_list[0]
print(first_element)
print(assorted_list)

5. Predict the outputs:

first_tuple = (3, 5, 7, 9)
print(first_tuple)

6. Predict the outputs:

another_set = {"red", "green", "black"}


print(type(another_set))
print(another_set)

7. Predict the outputs:

sample_dict = {"key":"value", "jan":31, "feb":28, "mar":31}

print(type(sample_dict))

print(sample_dict)

[Type here]

You might also like