Wednesday, May 3, 2023

#PYTHON(DAY7)List methods in python

                                             #PYTHON(DAY7)

Methods in Python

Python List Methods has multiple methods to work with Python lists

We use dir() function to print the built-in list methods

append()

  • append is used to add another element to the list and this element will be added at the end

Note: we can not append 2 elements into the list at a time

so, from the above code we get the error as list.append() takes exactly one value

extend()

  • extend is used to add two or more than two elements to end of the list
  • these values/elements should be passed in container

And the expected output for the above code is: [1,2,3,4,[5,6]]

count()

  • count will return the frequency of given element

And the expected output is:

insert()

  • insert as name suggests, is going to insert a new element at the specified index position
  • index takes two values i.e index and value

And the expected output is:

remove()

  • remove() is used to remove the specified element
  • it removes the first occurence

And the expected output is: here the first occurence is removed

pop()

  • pop removes the element at given index position, if no index value is passed, the last element will popped out
  • the discard/ejected element can be captured/assigned to a new for further use

And the expected output is:

Note: as the element is popped out, the index values changes

so, from the below code we get the error as pop index out of range

However, the remove() works on the elements and pop() works on indexes of elements

reverse()

  • it reverses the list

And the expected output is:

sort()

  • sorting in ascending or descending

And the expected output is:

And the expected output is:

copy()

  • copy is used to store the list in different location to stor it in the different location

clear()

  • it is going to erase all the elements from the list and returns an empty list

And the expected output is: [ ] (empty list)

Tuesday, May 2, 2023

#PYTHON(DAY6)List and list indexing in python

 

#PYTHON(DAY6)

List and list indexing in python

Lists

x = [1,2.2,3.3j,'four',print,str,True]
print(x,len(x),type(x))
print(x[0], x[-1], sep ='\n')

INDEX()

x = [1,2,3,4]
x.index(5)

INDEXING

Positive Indexing

INDEXING
thislist = ["apple", "banana", "cherry"]
print(thislist[1])

Negative Indexing

thislist = ["apple", "banana", "cherry"]
print(thislist[-1])

range

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
x = range(1,21,3)
y = range(20,0,-2)
#z = range(20.5,1.5,0.5) # can not access float numbers
print(x, id(x))
print(y, id(y))
x = list(x)
print(x, type(x))
print()

y = list(y)
print(y,type(y))
x = range(3, 20, 2)
for n in x:
print(n)

Monday, May 1, 2023

#PYTHON(DAY5) Data Types

 

#PYTHON(DAY5)

Data Types in python

Python Data Types are used to define the type of a variable. It defines what type of data we are going to store in a variable. The data stored in memory can be of many types. For example, a person’s age is stored as a numeric value and his or her address is stored as alphanumeric characters.

Python has various built-in data types which we will discuss with in this tutorial:

  • Numeric — int, float, complex
  • String — str
  • Sequence — list, tuple, range
  • Binary — bytes, bytearray, memoryview
  • Mapping — dict
  • Boolean — bool
  • Set — set, frozenset
  • None — NoneType
var1 = 1
var2 = 10
var3 = 20.5

Python supports four different numerical types −

  • int (signed integers)
  • long (long integers, they can also be represented in octal and hexadecimal)
  • float (floating point real values)
  • complex (complex numbers)

Examples

Here are some examples of numbers −

int : 10,20,-445,0x486

long : 51924361L,-0x19323L,0xDEFABCECBDAECBFBAEl,-052318172735L

float : 0.0,15.20,32.3+e18,-90.

complex : 3.14j,45.j,-.6545+0J

  • Python allows you to use a lowercase l with long, but it is recommended that you use only an uppercase L to avoid confusion with the number 1. Python displays long integers with an uppercase L.
  • A complex number consists of an ordered pair of real floating-point numbers denoted by x + yj, where x and y are the real numbers and j is the imaginary unit.

Example

Following is an example to show the usage of Integer, Float and Complex numbers:

# integer variable.
a=100
print("The type of variable having value", a, " is ", type(a))
# float variable.
b=20.345 print("The type of variable having value", b, " is ", type(b))
# complex variable.
c=10+3j print("The type of variable having value", c, " is ", type(c))

Python String Data Type

Python Strings are identified as a contiguous set of characters represented in the quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.

The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator in Python. For example −

str = 'Hello World!'

print (str) # Prints complete string
print (str[0]) # Prints first character of the string
print (str[2:5]) # Prints characters starting from 3rd to 5th
print (str[2:]) # Prints string starting from 3rd character
print (str * 2) # Prints string two times
print (str + "TEST") # Prints concatenated string
# output of above code
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST

In this story you will be finding only the numeric and string data types.

Building Static Website(part6) HTML Lists

  Building Static Website (part6) HTML Lists Today, let us add some lists to our detailed view section by using html lists. Lists: List is a...