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

Python Core Programming Part-1 Lab Guide

The document contains code snippets demonstrating basic Python programming concepts like variables, data types, lists, loops, and functions. It shows how to create and assign variables of different types like integers, floats, strings, booleans, lists, and dictionaries. It also demonstrates accessing, modifying, and looping through list elements using for and while loops. Functions are introduced via examples showing local and global scope.

Uploaded by

Akhilesh Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Python Core Programming Part-1 Lab Guide

The document contains code snippets demonstrating basic Python programming concepts like variables, data types, lists, loops, and functions. It shows how to create and assign variables of different types like integers, floats, strings, booleans, lists, and dictionaries. It also demonstrates accessing, modifying, and looping through list elements using for and while loops. Functions are introduced via examples showing local and global scope.

Uploaded by

Akhilesh Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Python Core Programming Part 1 Lab

Creating variable

x=5

y = "John"

print(x)

print(y)

x=4 # x is of type int

x = "Sally" # x is now of type str

print(x)

casting

x = str(3) # x will be '3'

y = int(3) # y will be 3

z = float(3) # z will be 3.0

x=5

y = "John"

print(type(x))

print(type(y))

variable names

myvar = "John"

my_var = "John"

_my_var = "John"

myVar = "John"

MYVAR = "John"
myvar2 = "John"

Program

2myvar = "John"

my-var = "John"

my var = "John"

Multiple variable

x, y, z = "Orange", "Banana", "Cherry"

print(x)

print(y)

print(z)

program

x = y = z = "Orange"

print(x)

print(y)

print(z)

program

fruits = ["apple", "banana", "cherry"]

x, y, z = fruits

print(x)

print(y)

print(z)

Variables

x = "Python is "

y = "awesome"

z= x+y
print(z)

Program

x=5

y = 10

print(x + y)

Global variables

x = "awesome"

def myfunc():

print("Python is " + x)

myfunc()

program

x = "awesome"

def myfunc():

x = "fantastic"

print("Python is " + x)

myfunc()

print("Python is " + x)

global keyboard

def myfunc():

global x
x = "fantastic"

myfunc()

print("Python is " + x)

Program

x = "awesome"

def myfunc():

global x

x = "fantastic"

myfunc()

print("Python is " + x)

Program

x = "Hello World"

#display x:

print(x)

#display the data type of x:

print(type(x))

Program-List

x = ["apple", "banana", "cherry"]

#display x:
print(x)

#display the data type of x:

print(type(x))

Program-Tuple

x = ("apple", "banana", "cherry")

#display x:

print(x)

program-range

x = range(6)

#display x:

print(x)

#display the data type of x:

print(type(x))

program-dict

x = {"name" : "John", "age" : 36}

#display x:

print(x)

#display the data type of x:

print(type(x))

program-set

x = {"apple", "banana", "cherry"}


#display x:

print(x)

#display the data type of x:

print(type(x))

#display the data type of x:

print(type(x))

Program-frozenset

x = frozenset({"apple", "banana", "cherry"})

#display x:

print(x)

#display the data type of x:

print(type(x))

program-bool

x = True

#display x:

print(x)

#display the data type of x:

print(type(x))

Program int

x=1
y = 35656222554887711

z = -3255522

print(type(x))

print(type(y))

print(type(z))

Program-float

x = 1.10

y = 1.0

z = -35.59

print(type(x))

print(type(y))

print(type(z))

Program-float

x = 35e3

y = 12E4

z = -87.7e100

print(type(x))

print(type(y))

print(type(z))

Program-Complex

x = 3+5j

y = 5j

z = -5j
print(type(x))

print(type(y))

print(type(z))

Program-strings

print("Hello")

print('Hello')

Program

a = "Hello"

print(a)

Program

a = """Lorem ipsum dolor sit amet,

consectetur adipiscing elit,

sed do eiusmod tempor incididunt

ut labore et dolore magna aliqua."""

print(a)

Program

a = '''Lorem ipsum dolor sit amet,

consectetur adipiscing elit,

sed do eiusmod tempor incididunt

ut labore et dolore magna aliqua.'''

print(a)

Program-Loop

for x in "banana":

print(x)

Program

a = "Hello, World!"

print(len(a))
Program

b = "Hello, World!"

print(b[2:5])

Program-List

thislist = ["apple", "banana", "cherry"]

print(thislist)

Program- List Item

list1 = ["apple", "banana", "cherry"]

list2 = [1, 5, 7, 9, 3]

list3 = [True, False, False]

Program

list1 = ["abc", 34, True, 40, "male"]

print(list1)

Program

mylist = ["apple", "banana", "cherry"]

print(type(mylist))

Pogram-Access List

thislist = ["apple", "banana", "cherry"]

print(thislist[1])

Program-Change list

thislist = ["apple", "banana", "cherry"]

thislist[1] = "blackcurrant"

print(thislist)

Program

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]

thislist[1:3] = ["blackcurrant", "watermelon"]


print(thislist)

Program

thislist = ["apple", "banana", "cherry"]

thislist[1:2] = ["blackcurrant", "watermelon"]

print(thislist)

Program-append

thislist = ["apple", "banana", "cherry"]

thislist.append("orange")

print(thislist)

program-insert

thislist = ["apple", "banana", "cherry"]

thislist.insert(1, "orange")

print(thislist)

Program-extend

thislist = ["apple", "banana", "cherry"]

tropical = ["mango", "pineapple", "papaya"]

thislist.extend(tropical)

print(thislist)

Program-Remove list

thislist = ["apple", "banana", "cherry"]

thislist.remove("banana")

print(thislist)

Program-Clear list

thislist = ["apple", "banana", "cherry"]

thislist.clear()

print(thislist)

Program-Loop
thislist = ["apple", "banana", "cherry"]

for x in thislist:

print(x)

Program-loop

thislist = ["apple", "banana", "cherry"]

for i in range(len(thislist)):

print(thislist[i])

Program-while loop

thislist = ["apple", "banana", "cherry"]

i=0

while i < len(thislist):

print(thislist[i])

i=i+1

You might also like