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

Python Basics

The document provides examples of Python code demonstrating basic programming concepts like variables, data types, operators, strings, tuples, lists, and dictionaries. It shows how to define and manipulate variables, check data types, perform arithmetic and logical operations, index and slice strings/lists/tuples, modify lists and dictionaries, and more. The code serves as an introduction to fundamental Python syntax and data structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Python Basics

The document provides examples of Python code demonstrating basic programming concepts like variables, data types, operators, strings, tuples, lists, and dictionaries. It shows how to define and manipulate variables, check data types, perform arithmetic and logical operations, index and slice strings/lists/tuples, modify lists and dictionaries, and more. The code serves as an introduction to fundamental Python syntax and data structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1/22/24, 11:17 PM Python+Basics

In [ ]: #Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distr

In [1]: #First Program


print("This is sparta!!!")

This is sparta!!!

In [2]: #Variables
var1="John"
print(var1)

John

In [3]: var1="Sam"
print(var1)

Sam

In [4]: var1="Matt"
print(var1)

Matt

In [2]: #Data-Type
a=10
type(a)

int
Out[2]:

In [3]: a=10.5
type(a)

float
Out[3]:

In [1]: a="sparta"
type(a)

str
Out[1]:

In [5]: a=True
type(a)

bool
Out[5]:

In [7]: a=3+4j
type(a)

complex
Out[7]:

In [ ]: #Arithmetic Operators

In [8]: a=10
b=20

In [9]: print(a+b)

30

In [10]: print(a-b)

-10

localhost:8889/nbconvert/html/Desktop/Python%2BBasics.ipynb?download=false 1/8
1/22/24, 11:17 PM Python+Basics

In [11]: print(a*b)

200

In [12]: print(a/b)

0.5

In [4]: #Relational Operators

In [5]: a=10
b=20

In [6]: a>b

False
Out[6]:

In [7]: a<b

True
Out[7]:

In [8]: a==b

False
Out[8]:

In [9]: a!=b

True
Out[9]:

In [10]: #Logical Operators

In [12]: a=True
b=False

In [14]: a&b

False
Out[14]:

In [15]: b&a

False
Out[15]:

In [16]: b&b

False
Out[16]:

In [17]: a&a

True
Out[17]:

In [18]: a|b

True
Out[18]:

In [19]: b|a

True
Out[19]:

localhost:8889/nbconvert/html/Desktop/Python%2BBasics.ipynb?download=false 2/8
1/22/24, 11:17 PM Python+Basics

In [20]: a|a

True
Out[20]:

In [21]: b|b

False
Out[21]:

In [22]: #Strings

In [23]: my_string="My name is John"

In [24]: my_string[0]

'M'
Out[24]:

In [5]: my_string="My name is John"

In [6]: my_string[-1]

'n'
Out[6]:

In [26]: my_string[0:4]

'My n'
Out[26]:

In [28]: len(my_string)

15
Out[28]:

In [30]: my_string.lower()

'my name is john'


Out[30]:

In [31]: my_string.upper()

'MY NAME IS JOHN'


Out[31]:

In [33]: my_string.replace('y','a')

'Ma name is John'


Out[33]:

In [7]: new_string = "hello hello world"

In [8]: new_string.count("hello")

2
Out[8]:

In [13]: s1 = 'This is sparta!!!'


s1.find('sparta')

8
Out[13]:

In [12]: s1.find('b')

1
Out[12]:

localhost:8889/nbconvert/html/Desktop/Python%2BBasics.ipynb?download=false 3/8
1/22/24, 11:17 PM Python+Basics

In [15]: fruit = 'I like apples, mangoes, bananas'

fruit.split(',')

['I like apples', ' mangoes', ' bananas']


Out[15]:

In [ ]: #Tuples in Python

In [18]: tup1=(1,"a",True,2,"b",False)

In [17]: tup1

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-17-afd04ff38ac4> in <module>
----> 1 tup1

NameError: name 'tup1' is not defined

In [44]: tup1[0]

1
Out[44]:

In [45]: tup1[-1]

False
Out[45]:

In [22]: tup1=(1,"a",True,2,"b",False)
tup1[1:4]

('a', True, 2)
Out[22]:

In [46]: tup1[1:4]

('a', True, 2)
Out[46]:

In [49]: tup1[2]="hello"

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-49-2fc16622751e> in <module>
----> 1 tup1[2]="hello"

TypeError: 'tuple' object does not support item assignment

In [50]: tup1[6]=3+4j

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-50-3b75c4b77e6a> in <module>
----> 1 tup1[6]=3+4j

TypeError: 'tuple' object does not support item assignment

In [52]: min(tup1)

localhost:8889/nbconvert/html/Desktop/Python%2BBasics.ipynb?download=false 4/8
1/22/24, 11:17 PM Python+Basics

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-52-ce68c930ff7f> in <module>
----> 1 min(tup1)

TypeError: '<' not supported between instances of 'str' and 'int'

In [24]: tup1=(1,"a",True,2,"b",False)
len(tup1)

6
Out[24]:

In [2]: tup1 = (1,"a",True)


tup2 = (4,5,6)

In [3]: tup2+tup1

(4, 5, 6, 1, 'a', True)


Out[3]:

In [31]: tup1 = ('sparta',300)


tup2 = (4,5,6)
tup1*3 + tup2

('sparta', 300, 'sparta', 300, 'sparta', 300, 4, 5, 6)


Out[31]:

In [32]: tup1=(1,2,3,4,5)
min(tup1)

1
Out[32]:

In [33]: tup1=(1,2,3,4,5)
max(tup1)

5
Out[33]:

In [34]: cmp(tup1,tup2)

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-34-e27d9faf1f7f> in <module>
----> 1 cmp(tup1,tup2)

NameError: name 'cmp' is not defined

In [53]: #List in Python

In [ ]:

In [56]: l1=[1,"a",2,"b",3,"c"]

In [58]: l1=[1,"a",2,"b",3,"c"]
l1[1]

'a'
Out[58]:

In [59]: l1=[1,"a",2,"b",3,"c"]
l1[2:5]

localhost:8889/nbconvert/html/Desktop/Python%2BBasics.ipynb?download=false 5/8
1/22/24, 11:17 PM Python+Basics
[2, 'b', 3]
Out[59]:

In [35]: l1=[1,"a",2,"b",3,"c"]
l1[0]=100
l1

[100, 'a', 2, 'b', 3, 'c']


Out[35]:

In [36]: l1=[1,"a",2,"b",3,"c"]
l1.append("Sparta")
l1

[1, 'a', 2, 'b', 3, 'c', 'Sparta']


Out[36]:

In [62]: l1

[100, 'a', 2, 'b', 3, 'c', True]


Out[62]:

In [37]: l1=[1,"a",2,"b",3,"c"]
l1.pop()
l1

[1, 'a', 2, 'b', 3]


Out[37]:

In [38]: l1

[1, 'a', 2, 'b', 3]


Out[38]:

In [41]: l1=[1,"a",2,"b",3,"c"]
l1.insert(1,"Sparta")
l1

[1, 'Sparta', 'a', 2, 'b', 3, 'c']


Out[41]:

In [43]: l1 = ["mango","banana","guava","apple"]
l1.sort()
l1

['apple', 'banana', 'guava', 'mango']


Out[43]:

In [44]: l1 = [1,2,3]
l2 = ["a","b","c"]
l1+l2

[1, 2, 3, 'a', 'b', 'c']


Out[44]:

In [45]: l1 = [1,"a",True]
l1*3

[1, 'a', True, 1, 'a', True, 1, 'a', True]


Out[45]:

In [ ]: #Dictionary in Python

In [68]: fruit={"Apple":10,"Orange":20,"Banana":30,"Guava":40}

In [1]: fruit={"Apple":10,"Orange":20,"Banana":30,"Guava":40}
fruit.keys()

localhost:8889/nbconvert/html/Desktop/Python%2BBasics.ipynb?download=false 6/8
1/22/24, 11:17 PM Python+Basics
dict_keys(['Apple', 'Orange', 'Banana', 'Guava'])
Out[1]:

In [70]: fruit={"Apple":10,"Orange":20,"Banana":30,"Guava":40}
fruit.values()

dict_values([10, 20, 30, 40])


Out[70]:

In [74]: fruit["Apple"]

10
Out[74]:

In [2]: fruit={"Apple":10,"Orange":20,"Banana":30,"Guava":40}
fruit["Mango"]=50
fruit

{'Apple': 10, 'Orange': 20, 'Banana': 30, 'Guava': 40, 'Mango': 50}
Out[2]:

In [3]: fruit={"Apple":10,"Orange":20,"Banana":30,"Guava":40,"Mango":50}
fruit["Apple"]=100
fruit

{'Apple': 100, 'Orange': 20, 'Banana': 30, 'Guava': 40, 'Mango': 50}
Out[3]:

In [4]: fruit1={"Apple":10,"Orange":20}
fruit2={"Banana":30,"Guava":40}

fruit1.update(fruit2)

fruit1

{'Apple': 10, 'Orange': 20, 'Banana': 30, 'Guava': 40}


Out[4]:

In [6]: fruit={"Apple":10,"Orange":20,"Banana":30,"Guava":40}
fruit.pop("Orange")
fruit

{'Apple': 10, 'Banana': 30, 'Guava': 40}


Out[6]:

In [78]: #Set in Python

In [1]: s1={1,"a",True,2,"b",False}
s1

{1, 2, False, 'a', 'b'}


Out[1]:

In [7]: s1={1,"a",True,2,"b",False}
s1.add("Hello")
s1

{1, 2, False, 'Hello', 'a', 'b'}


Out[7]:

In [8]: s1={1,"a",True,2,"b",False}
s1.update([10,20,30])
s1

{1, 10, 2, 20, 30, False, 'a', 'b'}


Out[8]:

In [9]: s1={1,"a",True,2,"b",False}
s1.remove("b")
localhost:8889/nbconvert/html/Desktop/Python%2BBasics.ipynb?download=false 7/8
1/22/24, 11:17 PM Python+Basics
s1

{1, 2, False, 'a'}


Out[9]:

In [13]: s1 = {1,2,3,4,5,6}
s2 = {5,6,7,8,9}

s1.intersection(s2)

{5, 6}
Out[13]:

In [ ]:

localhost:8889/nbconvert/html/Desktop/Python%2BBasics.ipynb?download=false 8/8

You might also like