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

Python Basics, String, List

The document provides an overview of Python, a high-level programming language developed by Guido van Rossum in 1991. It covers fundamental concepts such as keywords, variables, data types (numeric, boolean, string, list), and basic operations including string functions and list methods. The content is structured with code examples to illustrate the usage of these concepts in Python programming.
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 views14 pages

Python Basics, String, List

The document provides an overview of Python, a high-level programming language developed by Guido van Rossum in 1991. It covers fundamental concepts such as keywords, variables, data types (numeric, boolean, string, list), and basic operations including string functions and list methods. The content is structured with code examples to illustrate the usage of these concepts in Python programming.
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

04/07/2025, 17:21 Python Basics, Datatypes and Operators

Python
Python is an object-oriented, high-level, interpreted, general
purpose and dynamic programming language.

It was developed by Guido van Rossum in 1991.

The name 'Python' came from an old BBC television comedy sketch
series "Monty Python's Flying Circus".

In [1]: #Simple python program


print("Hello Summer🍁")

Hello Summer🍁

In [2]: print('Debashree Sahoo🍨')

Debashree Sahoo🍨

Keywords : Reserved Words (pre-defined


words)
In [3]: #to get python keywords
help('keywords')

Here is a list of the Python keywords. Enter any keyword to get more help.

False class from or


None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not

In [4]: help('if')

localhost:8888/doc/tree/Desktop/AI/Python Basics%2C Datatypes and [Link] 1/23


04/07/2025, 17:21 Python Basics, Datatypes and Operators

The "if" statement


******************

The "if" statement is used for conditional execution:

if_stmt ::= "if" assignment_expression ":" suite


("elif" assignment_expression ":" suite)*
["else" ":" suite]

It selects exactly one of the suites by evaluating the expressions one


by one until one is found to be true (see section Boolean operations
for the definition of true and false); then that suite is executed
(and no other part of the "if" statement is executed or evaluated).
If all expressions are false, the suite of the "else" clause, if
present, is executed.

Related help topics: TRUTHVALUE

Variable : variables are the names given to


the memory location to store a value or it
is a container to store a value
We can use upper case letters(A-Z), lower case letters (a-z),
digits(0-9)

We can't use special symbols except underscore(_)

We can't use keywords

We can't start with a digit

We can't use space in between the variable name instead we can use
underscore.

Python is case sensitive

In [5]: #we cant't use keyword as variable name


for = 10

Cell In[5], line 2


for = 10
^
SyntaxError: invalid syntax

In [6]: #we can't start with a digit


123var = 20

localhost:8888/doc/tree/Desktop/AI/Python Basics%2C Datatypes and [Link] 2/23


04/07/2025, 17:21 Python Basics, Datatypes and Operators

Cell In[6], line 2


123var = 20
^
SyntaxError: invalid decimal literal

In [7]: #we can't use space in between the variable name instead we can use underscore.
var abc = 30

Cell In[7], line 2


var abc = 30
^
SyntaxError: invalid syntax

In [8]: #we can't use special symbols except underscore(_)


@!$% = 40

Cell In[8], line 2


@!$% = 40
^
SyntaxError: invalid syntax

In [10]: #we can use upper case letters(A-Z), lower case letters (a-z), digits(0-9)
VAR = 50
print(VAR)

50

In [12]: var = 60
print(var)

60

In [13]: Var_123 = 100


print(Var_123)

100

In [ ]:

Datatypes
Primary
Numeric :
Integer - int
Float - float
Complex - complex

Boolean(bool) :
True
False

localhost:8888/doc/tree/Desktop/AI/Python Basics%2C Datatypes and [Link] 3/23


04/07/2025, 17:21 Python Basics, Datatypes and Operators

Sequential
String - str
List - list
Tuple - tuple
Set - set
Dictionary - dict

In [14]: #int
a = 10
print(a)

10

In [15]: type(a)

Out[15]: int

In [16]: #float
f = 4.5
print(f)

4.5

In [17]: type(f)

Out[17]: float

In [18]: #complex
c = 2+5i
print(c)

Cell In[18], line 2


c = 2+5i
^
SyntaxError: invalid decimal literal

In [19]: c = 2+5j
print(c)

(2+5j)

In [20]: type(c)

Out[20]: complex

In [21]: #Boolean
b = True
print(b)

True

In [22]: type(b)

localhost:8888/doc/tree/Desktop/AI/Python Basics%2C Datatypes and [Link] 4/23


04/07/2025, 17:21 Python Basics, Datatypes and Operators

Out[22]: bool

In [ ]:

String (str) : sequence of characters stored inside quotation


marks
In [40]: a = "Hello Everyone"
print(a)

Hello Everyone

In [41]: type(a)

Out[41]: str

In [37]: b = 'abc123@#$%🍟🍔🍕'
print(b)
type(b)

abc123@#$%🍟🍔🍕
Out[37]: str

In [ ]:

In [38]: #single line string denoted by ' ' or " "


a = 'this is a single line string'
b = "this is a single line string"

print(a)
print(b)

this is a single line string


this is a single line string

In [44]: type(a),type(b)

Out[44]: (str, str)

In [39]: #multi line string is denoted with ''' ''' or """ """
a = '''this is a
multi line'''
b = """this is a
multi line string"""

print(a)
print(b)

this is a
multi line
this is a
multi line string

In [47]: type(a),type(b)

localhost:8888/doc/tree/Desktop/AI/Python Basics%2C Datatypes and [Link] 5/23


04/07/2025, 17:21 Python Basics, Datatypes and Operators

Out[47]: (str, str)

In [42]: a = "Tasty Apple"


print(a)
type(a)

Tasty Apple
Out[42]: str

In [43]: #indexing
#Syntax : var[index_pos]
a[4]

Out[43]: 'y'

In [44]: a[-7]

Out[44]: 'y'

In [45]: #slicing
#Forward Slicing
#syntax = var[start:stop+1:step]

#'sty A'
a[2:7:1]

Out[45]: 'sty A'

In [46]: a[-9:-4:1]

Out[46]: 'sty A'

In [47]: #"TsyApe"
a[0:11:2]

Out[47]: 'TsyApe'

In [51]: a[-11::2]

Out[51]: 'TsyApe'

In [52]: a[-11:11:2]

Out[52]: 'TsyApe'

In [53]: #"Ttal"
a[0:10:3]

Out[53]: 'TtAl'

In [54]: a[-11:-1:3]

localhost:8888/doc/tree/Desktop/AI/Python Basics%2C Datatypes and [Link] 6/23


04/07/2025, 17:21 Python Basics, Datatypes and Operators

Out[54]: 'TtAl'

In [ ]:

In [55]: a[::]

Out[55]: 'Tasty Apple'

In [56]: a[::-1]

Out[56]: 'elppA ytsaT'

In [ ]:

In [57]: #Forward Slicing


#syntax = var[start:stop+1:step]
#Backward SLicing
#syntax = var[start:stop-1:step]

#"A yts"
a[6:1:-1]

Out[57]: 'A yts'

In [58]: a[-5:-10:-1]

Out[58]: 'A yts'

In [ ]:

localhost:8888/doc/tree/Desktop/AI/Python Basics%2C Datatypes and [Link] 7/23


04/07/2025, 17:21 Python Basics, Datatypes and Operators

String Functions
capitalize() : converts the first character to upper case and the
rest lower
case.

upper() : converts all the characters into upper case

lower() : converts all the characters into lower case

casefold() : converts all the characters into lower case

center() : returns a centered string

count() : it counts how many times a particular character is


repeated

index() : returns index position of a particular character

strip() : removes spaces present before and after the string

In [76]: a = 'heLlO eVeRYoNE'


print(a)

heLlO eVeRYoNE

In [78]: [Link]()

Out[78]: 'Hello everyone'

In [79]: [Link]()

Out[79]: 'HELLO EVERYONE'

In [80]: [Link]()

Out[80]: 'hello everyone'

In [81]: [Link]()

Out[81]: 'hello everyone'

In [82]: [Link]()

Out[82]: 'HElLo EvEryOne'

In [83]: [Link]()

Out[83]: 'Hello Everyone'

localhost:8888/doc/tree/Desktop/AI/Python Basics%2C Datatypes and [Link] 8/23


04/07/2025, 17:21 Python Basics, Datatypes and Operators

In [84]: [Link](50)

Out[84]: ' heLlO eVeRYoNE '

In [86]: [Link](50,'🍕')

Out[86]: '🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕heLlO eVeRYoNE🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕🍕


🍕🍕'
In [87]: [Link]('e')

Out[87]: 3

In [88]: [Link]('e')

Out[88]: 1

In [89]: '😊'.join(['john','[Link]','sofy'])

Out[89]: 'john😊[Link]😊sofy'

In [83]: 'ram sita gita'.split()

Out[83]: ['ram', 'sita', 'gita']

In [93]: 'ram_sita_gita'.split('_')

Out[93]: ['ram', 'sita', 'gita']

In [94]: ' hello '.strip()

Out[94]: 'hello'

In [98]: '*****%%%*%*%*hello****%%****'.strip('*%')

Out[98]: 'hello'

In [92]: ' hello '.lstrip()

Out[92]: 'hello '

In [93]: ' hello '.rstrip()

Out[93]: ' hello'

In [99]: 'ABC'.isalpha()

Out[99]: True

In [100… '123'.isdigit()

Out[100… True

localhost:8888/doc/tree/Desktop/AI/Python Basics%2C Datatypes and [Link] 9/23


04/07/2025, 17:21 Python Basics, Datatypes and Operators

In [101… '123'.isnumeric()

Out[101… True

In [102… 'abc123'.isalnum()

Out[102… True

In [103… 'The Fairy Tale'.istitle()

Out[103… True

In [104… 'var abc'.isidentifier()

Out[104… False

In [105… 'Var_123'.isidentifier()

Out[105… True

In [ ]:

LIST (list)
Sequence of items denoted with '[]' and separated with ','.

It is ordered(indexed with specific position), mutable(items can be


changed) and heterogenous(supports multiple data-type).

In [106… l = [20,2.5,'cat',True]
print(l)

[20, 2.5, 'cat', True]

In [107… type(l)

Out[107… list

In [108… #indexing syntax : var[index]


l[2]

Out[108… 'cat'

In [109… l[-2]

Out[109… 'cat'

In [110… #slicing syntax : var[start : stop+1 : step]


l[0:3:2]

localhost:8888/doc/tree/Desktop/AI/Python Basics%2C Datatypes and [Link] 10/23


04/07/2025, 17:21 Python Basics, Datatypes and Operators

Out[110… [20, 'cat']

In [111… l[::]

Out[111… [20, 2.5, 'cat', True]

In [112… l[::-1]

Out[112… [True, 'cat', 2.5, 20]

In [ ]:

In [ ]:

List Methods
Adding Items to a List
[Link](obj) : add item to the end of the list
[Link](ind,obj) : add item to the given index of the list
[Link]([sequence]) : add items from the given sequence at the end of the list

Removing Items from the List


[Link](obj) : removes the first matched instance of object from the list
[Link](index) : it pop or delete the item of the given index from the list
[Link]() : empty the list

Miscellaneous methods
[Link]() : copy the item
[Link](obj)
[Link](obj)
[Link]() : sort or arrange in ascending order
[Link](reverse=True) : arrange in descending order
[Link]() or var[ : : -1] can be use to reverse the list

In [116… l = [25,67,25,89,35]
print(l)
type(l)

[25, 67, 25, 89, 35]


Out[116… list

In [117… #adding items to a list


#append
print(l)

localhost:8888/doc/tree/Desktop/AI/Python Basics%2C Datatypes and [Link] 11/23


04/07/2025, 17:21 Python Basics, Datatypes and Operators

[Link](100)
print(l)

[25, 67, 25, 89, 35]


[25, 67, 25, 89, 35, 100]

In [118… #insert
print(l)
[Link](2,65)
print(l)

[25, 67, 25, 89, 35, 100]


[25, 67, 65, 25, 89, 35, 100]

In [119… #extend
print(l)
[Link]([20,30,40])
print(l)

[25, 67, 65, 25, 89, 35, 100]


[25, 67, 65, 25, 89, 35, 100, 20, 30, 40]

In [ ]:

In [120… #removing elements from a list


#pop
print(l)
[Link]()
print(l)

[25, 67, 65, 25, 89, 35, 100, 20, 30, 40]
[25, 67, 65, 25, 89, 35, 100, 20, 30]

In [121… print(l)
[Link](2)
print(l)

[25, 67, 65, 25, 89, 35, 100, 20, 30]


[25, 67, 25, 89, 35, 100, 20, 30]

In [122… #remove
print(l)
[Link](25)
print(l)

[25, 67, 25, 89, 35, 100, 20, 30]


[67, 25, 89, 35, 100, 20, 30]

In [66]: #clear

In [ ]:

In [123… #Miscellaneous methods in list


#copy
print(l)
m = [Link]()
print(m)

localhost:8888/doc/tree/Desktop/AI/Python Basics%2C Datatypes and [Link] 12/23


04/07/2025, 17:21 Python Basics, Datatypes and Operators

[67, 25, 89, 35, 100, 20, 30]


[67, 25, 89, 35, 100, 20, 30]

In [124… #count
[Link](35)

Out[124… 1

In [125… #index
[Link](100)

Out[125… 4

In [126… #sort - ascending order


print(l)
[Link]()
print(l)

[67, 25, 89, 35, 100, 20, 30]


[20, 25, 30, 35, 67, 89, 100]

In [130… #sort string


a = ['gun','ball','sky','apple']
print(a)
[Link]()
print(a)

['gun', 'ball', 'sky', 'apple']


['apple', 'ball', 'gun', 'sky']

In [131… a = [10,45,'gun','ball','sky','apple']
print(a)
[Link]()
print(a)

[10, 45, 'gun', 'ball', 'sky', 'apple']


---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[131], line 3
1 a = [10,45,'gun','ball','sky','apple']
2 print(a)
----> 3 [Link]()
4 print(a)

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

In [ ]: a = ['10','gun','25',,'ball','sky','apple']
print(a)
[Link]()
print(a)

In [127… print(l)
[Link](reverse=True) #descending order
print(l)

localhost:8888/doc/tree/Desktop/AI/Python Basics%2C Datatypes and [Link] 13/23


04/07/2025, 17:21 Python Basics, Datatypes and Operators

[20, 25, 30, 35, 67, 89, 100]


[100, 89, 67, 35, 30, 25, 20]

In [ ]:

In [128… #reverse
a = [13,78,45,90,23]
print(a)
[Link]()
print(a)

[13, 78, 45, 90, 23]


[23, 90, 45, 78, 13]

In [129… #clear
print(l)
[Link]()
print(l)

[100, 89, 67, 35, 30, 25, 20]


[]

In [ ]:

Tuple (tuple)
An ordered but immutable(which cannot change size or permanent)
collection of items.

It is denoted by parenthsis '()' and separated by ','.

In [ ]:

In [ ]:

In [72]: #indexing

In [73]: #slicing

In [ ]:

Tuple Methods
In [74]: #count

In [75]: #index

In [ ]:

localhost:8888/doc/tree/Desktop/AI/Python Basics%2C Datatypes and [Link] 14/23

You might also like