0% found this document useful (0 votes)
2 views6 pages

Lesson 01 - Python Basics

The document provides an overview of basic Python programming concepts, including variable naming conventions, data types, and operators. It covers examples of strings, lists, tuples, dictionaries, and various operators such as arithmetic, comparison, and logical operators. Additionally, it explains type conversion and membership operators with practical examples.

Uploaded by

Vikram Gupta
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)
2 views6 pages

Lesson 01 - Python Basics

The document provides an overview of basic Python programming concepts, including variable naming conventions, data types, and operators. It covers examples of strings, lists, tuples, dictionaries, and various operators such as arithmetic, comparison, and logical operators. Additionally, it explains type conversion and membership operators with practical examples.

Uploaded by

Vikram Gupta
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

In [1]: # First Python Command

print ("Hello World!!!")

Hello World!!!

In [2]: # Python Variable Naming Convention


# 1. Python is a Case Sensitive language
x = 10
X = 202
print (x)
print (X)

10
202

In [3]: # 2. Python variables can have letters, numbers and underscores only.
# The variable name can begin with a letter or an underscore
#x$ = 2 #invalid
#2x = 3 #invalid
_x = 6 #valid
x_2 = 5 #valid
#x_$ = 9 #invalid

In [4]: # String Quotation


word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""
print (word)
print (sentence)
print (paragraph)

word
This is a sentence.
This is a paragraph. It is
made up of multiple lines and sentences.

In [5]: # Assigning Values to Variables using '=' sign


counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string

print (counter)
print (miles)
print (name)

100
1000.0
John

In [6]: # Multiple Assignment


a = b = c = 1
print(a)
print(b)
print(c)

1
1
1

In [7]: a,b,c = 1,2,"john"


print(a)
print(b)
print(c)

1
2
john

In [8]: # Python Data Types


# Numeric, String, Boolean, Lists, Tuples, Dictionary

/
In [4]: # Numbers
x = 100
y = 187687654564658970978909869576453
z = 123.45
h = 3 + 4j
print(x)
print(y)
print(z)
print(h)
print(type(x))
print(type(y))
print(type(z))
print(type(h))

100
187687654564658970978909869576453
123.45
(3+4j)
<class 'int'>
<class 'int'>
<class 'float'>
<class 'complex'>

In [5]: # Strings
mystr = 'Hello World!'

print (mystr) # Prints complete string


print (mystr[0]) # Prints first character of the string
print (mystr[2:5]) # Prints characters starting from 3rd to 5th
print (mystr[2:]) # Prints string starting from 3rd character
print (mystr * 2) # Prints string two times
print (mystr + "TEST") # Prints concatenated string

Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST

In [6]: # Boolean
pos_val = True
neg_val = False

print(pos_val)
print(neg_val)

True
False

In [11]: # Lists
mylist = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']

print (mylist) # Prints complete list


print (mylist[0]) # Prints first element of the list
print (mylist[1:3]) # Prints elements starting from 2nd till 3rd
print (mylist[2:]) # Prints elements starting from 3rd element
print (tinylist * 2) # Prints list two times
print (mylist + tinylist) # Prints concatenated lists
print (mylist[-1]) # Prints the last item in the list

# Adding elements to the list


[Link]('new value')
print(mylist)
[Link](['some more values', '1', '2'])
print(mylist)

['abcd', 786, 2.23, 'john', 70.2]


abcd
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.2, 123, 'john']
70.2
['abcd', 786, 2.23, 'john', 70.2, 'new value']
['abcd', 786, 2.23, 'john', 70.2, 'new value', 'some more values', '1', '2']

/
In [10]: # Tuples
mytuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')

print (mytuple) # Prints complete tuple


print (mytuple[0]) # Prints first element of the tuple
print (mytuple[1:3]) # Prints elements starting from 2nd till 3rd
print (mytuple[2:]) # Prints elements starting from 3rd element
print (tinytuple * 2) # Prints tuple two times
print (mytuple + tinytuple) # Prints concatenated tuple
print (mytuple[-1]) # Prints the last item in the tuple

('abcd', 786, 2.23, 'john', 70.2)


abcd
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.2, 123, 'john')
70.2

In [12]: # Following is invalid with tuple


tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tuple[2] = 1000 # Invalid syntax with tuple
list[2] = 1000 # Valid syntax with list
list

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-84d7d4045c82> in <module>
2 tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
3 list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
----> 4 tuple[2] = 1000 # Invalid syntax with tuple
5 list[2] = 1000 # Valid syntax with list
6 list

TypeError: 'tuple' object does not support item assignment

In [15]: # Dictionary
#mydict['one'] = "This is one"
#mydict[2] = "This is two"

tinydict = {'name': 'john','code':6734, 'dept': 'sales'}

print (tinydict['name'] )
print (tinydict['code'])
print (tinydict) # Prints complete dictionary
print ([Link]()) # Prints all the keys
print ([Link]()) # Prints all the values
print ([Link]('dept')) # Prints the value associated with the key
print ([Link]()) # Prints all the elements

john
6734
{'name': 'john', 'code': 6734, 'dept': 'sales'}
dict_keys(['name', 'code', 'dept'])
dict_values(['john', 6734, 'sales'])
sales
dict_items([('name', 'john'), ('code', 6734), ('dept', 'sales')])

In [16]: # Type Conversion


a = 10
b = 23.7

print(float(a)) #converts to float


print(int(b)) #converts to int

print(repr(a)) #converts to string


print(type(repr(a)))

print(eval('a + 1')) #evaluates an expression

10.0
23
10
<class 'str'>
11

Operators

/
In [17]: # Arithmetic Operators
a = 21
b = 10
c = 0

c = a + b
print ("Line 1 - Value of c is ", c)

c = a - b
print ("Line 2 - Value of c is ", c )

c = a * b
print ("Line 3 - Value of c is ", c )

c = a / b
print ("Line 4 - Value of c is ", c )

c = a % b # returns remainder
print ("Line 5 - Value of c is ", c)

a = 2
b = 3
c = a**b # Performs exponential (power) a to the power b
print ("Line 6 - Value of c is ", c)

a = 10
b = 5
c = a//b # returns the quotient
print ("Line 7 - Value of c is ", c)

Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2.1
Line 5 - Value of c is 1
Line 6 - Value of c is 8
Line 7 - Value of c is 2

In [18]: # Comparison Operator


a = 21
b = 10
c = 0

if ( a == b ):
print ("Line 1 - a is equal to b")
else:
print ("Line 1 - a is not equal to b")

if ( a != b ):
print ("Line 2 - a is not equal to b")
else:
print ("Line 2 - a is equal to b")

if ( a < b ):
print ("Line 3 - a is less than b" )
else:
print ("Line 3 - a is not less than b")

if ( a > b ):
print ("Line 4 - a is greater than b")
else:
print ("Line 4 - a is not greater than b")

a = 5;
b = 20;
if ( a <= b ):
print ("Line 5 - a is either less than or equal to b")
else:
print ("Line 5 - a is neither less than nor equal to b")

if ( b >= a ):
print ("Line 6 - b is either greater than or equal to b")
else:
print ("Line 6 - b is neither greater than nor equal to b")

Line 1 - a is not equal to b


Line 2 - a is not equal to b
Line 3 - a is not less than b
Line 4 - a is greater than b
Line 5 - a is either less than or equal to b
Line 6 - b is either greater than or equal to b

/
In [19]: # Assignment Operator
a = 21
b = 10
c = 0

c = a + b
print ("Line 1 - Value of c is ", c)

c += a
print ("Line 2 - Value of c is ", c)

c *= a
print ("Line 3 - Value of c is ", c )

c /= a
print ("Line 4 - Value of c is ", c )

c = 2
c %= a
print ("Line 5 - Value of c is ", c)

c **= a # c to the power of a


print ("Line 6 - Value of c is ", c)

c //= a
print ("Line 7 - Value of c is ", c)

Line 1 - Value of c is 31
Line 2 - Value of c is 52
Line 3 - Value of c is 1092
Line 4 - Value of c is 52.0
Line 5 - Value of c is 2
Line 6 - Value of c is 2097152
Line 7 - Value of c is 99864

In [20]: # Bitwise Operators


a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0

c = a & b; # 12 = 0000 1100


print ("Line 1 - Value of c is ", c)

c = a | b; # 61 = 0011 1101
print ("Line 2 - Value of c is ", c)

c = a ^ b; # 49 = 0011 0001 # XOR - It copies the bit if it is set in one operand but not both.
print ("Line 3 - Value of c is ", c)

c = ~a; # -61 = 1100 0011 # Ones Complement. It is unary and has the effect of 'flipping' bits.
print ("Line 4 - Value of c is ", c)

c = a << 2; # 240 = 1111 0000 # Binary Left Shift - The left operands value is moved left by the number of bits
specified by the right operand.
print ("Line 5 - Value of c is ", c)

c = a >> 2; # 15 = 0000 1111 # Binary Right Shift - The left operands value is moved right by the number of bi
ts specified by the right operand.
print ("Line 6 - Value of c is ", c)

Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15

In [21]: # Logical Operators


a = True
b = False

c = a and b
print ("Line 1 - Value of c is ", c)

c = a or b
print ("Line 2 - Value of c is ", c)

c = not a
print ("Line 3 - Value of c is ", c )

Line 1 - Value of c is False


Line 2 - Value of c is True
Line 3 - Value of c is False

/
In [22]: # Membership Operators

a = 10
b = 20
list = [1, 2, 3, 4, 5 ];

if ( a in list ):
print ("Line 1 - a is available in the given list")
else:
print ("Line 1 - a is not available in the given list")

if ( b not in list ):
print ("Line 2 - b is not available in the given list")
else:
print ("Line 2 - b is available in the given list")

a = 2
if ( a in list ):
print ("Line 3 - a is available in the given list")
else:
print ("Line 3 - a is not available in the given list")

Line 1 - a is not available in the given list


Line 2 - b is not available in the given list
Line 3 - a is available in the given list

In [23]: # Identity Operators


a = 20
b = 20

if ( a is b ):
print ("Line 1 - a and b have same identity")
else:
print ("Line 1 - a and b do not have same identity")

if ( id(a) == id(b) ):
print ("Line 2 - a and b have same identity")
else:
print ("Line 2 - a and b do not have same identity")

b = 30
if ( a is b ):
print ("Line 3 - a and b have same identity")
else:
print ("Line 3 - a and b do not have same identity")

if ( a is not b ):
print ("Line 4 - a and b do not have same identity")
else:
print ("Line 4 - a and b have same identity")

Line 1 - a and b have same identity


Line 2 - a and b have same identity
Line 3 - a and b do not have same identity
Line 4 - a and b do not have same identity

In [24]: # Operator Precedence


a = 20
b = 10
c = 15
d = 5
e = 0

e = (a + b) * c / d #( 30 * 15 ) / 5
print ("Value of (a + b) * c / d is ", e)

e = ((a + b) * c) / d # (30 * 15 ) / 5
print ("Value of ((a + b) * c) / d is ", e)

e = (a + b) * (c / d); # (30) * (15/5)


print ("Value of (a + b) * (c / d) is ", e)

e = a + (b * c) / d; # 20 + (150/5)
print ("Value of a + (b * c) / d is ", e)

Value of (a + b) * c / d is 90.0
Value of ((a + b) * c) / d is 90.0
Value of (a + b) * (c / d) is 90.0
Value of a + (b * c) / d is 50.0

In [ ]:

You might also like