Tuesday, 5 March 2024

Python Learning Session –06 - Sting Data Type

string data type in Python:

  introduction about the string data type in python 

String is nothing but is only set of character i.e “Techshoot03” it set of alphabet characters “ T,e,c,h,s,h,o,o,0,3 ” . All the set of character are get to gather and make one word and set of words are call String.

 There are so many Data type are available in the programing all languages are same data type are used

 Lest we seen how many data type are available in programing.

·         Intrigue

·         Boolean

·         String

·         Double

·         Float

·         Complex

·         List

·         Tuple

·         Dict

·         Set

“Mastering Python Strings: A Comprehensive Guide”

Dive deep into string manipulation, slicing, and formatting techniques.

Cover common use cases and best practices for handling strings.

 

“Python String Methods Every Developer Should Know”

Explore essential string methods like split()join()replace(), and more.

Provide practical examples and tips for efficient string processing.

 

“Formatting Strings in Python: A Detailed Overview”

Discuss f-strings, .format(), and % formatting.

Explain when to use each method and their pros/cons.

 

“Exploring Python’s Built-in String Functions”

Highlight lesser-known string functions like isalpha()startswith(), and count().

Showcase their applications and performance considerations.

 

“String Manipulation in Python: Tips and Tricks for Efficiency”

Share tricks for optimizing string concatenation, searching, and case conversion.

Address memory usage and performance bottlenecks.

 

Example : -

Code : - a = “hello techshoot03”

Print (a)                   --  then press the enter and check the result as we seen in the snap

 Looking Inside Strings :

We can get at any single character in a string using an index specified in square brackets

The index value must be an integer and starts at zero

The index value can be an expression that is computed

Lest we more undartanding we go throw the code :

Code : -

>>> a = "hello Techshoot03"

>>> print ( a )

hello Techshoot03

>>> 

>>> a = 3

>>> b = a [a-1]

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: 'int' object is not subscriptable

>>> demo = "hello techshoot03"

>>> test = demo[1]

>>> print (test)

e

>>> a = 3

>>> b = demo[a-1]

>>> print(b)

l

>>> 

The built-in function len gives us the length of a string

Code : -

 >>> demo = "hello techshoot03"

>>> print(len(demo))

17

>>> 


LEN A function is some stored code that we use. A function takes some input and produces an output.

Looping and Counting of Strings : -

This is a simple loop that loops through each letter in a string and counts the number of times the loop encounters the 'a' character

Slicing Strings: -

We can also look at any continuous section of a string using a colon operator

The second number is one beyond the end of the slice – “up to but not including”

If the second number is beyond the end of the string, it stops at the end

If we leave off the first number or the last number of the slice, it is assumed to be the beginning or end of the string respectively

 When the  +  operator is applied to strings, it means “concatenation”



The in keyword can also be used to check to see if one string is “in” another string

The in expression is a logical expression that returns True or False and can be used in an if statement



String Comparison :




String Library: -

·         str.capitalize()

·         str.center(width[, fillchar])

·         str.endswith(suffix[, start[, end]])

·         str.find(sub[, start[, end]])

·         str.lstrip([chars])

·         str.replace(old, new[, count])

·         str.lower()

·         str.rstrip([chars])

·         str.strip([chars])

·         str.upper()

Searching a String : -

We use the find() function to search for a substring within another string

find() finds the first occurrence of the substring

If the substring is not found, find() returns -1

Remember that string position starts at zero

 

Code : -

>>>demo = 'Techshoot03'

>>> test = demo.find('Te')

>>> print(test)

1 - --  Result

 

You can make a copy of a string in lower case or upper case

Often when we are searching for a string using find() we first convert the string to lower case so we can search a string regardless of case

The replace() function is like a “search and replace” operation in a word processor

It replaces all occurrences of the search string with the replacement string

Sometimes we want to take a string and remove whitespace at the beginning and/or end

lstrip() and rstrip() remove whitespace at the left or right

strip() removes both beginning and ending whitespace

 

Tuesday, 20 February 2024

Python Learning Session –05- Loops and Iteration

 

Loops

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.

Loops (repeated steps) have iteration variables that change each time through a loop.  Often these iteration variables go through a sequence of numbers.

There are so many type of looping statement are available to doing various operations

 

Python has two primitive loop commands:

  • while loops
  • for loops

 

While Loops Are:

·         The while Loop

·         The break Statement

·         The continue Statement

·         The else Statement

For Loops Are: -

·         Looping Through a String

·         The break Statement

·         The continue Statement

·         The range() Function

·         Else in For Loop

·         Nested Loops

·         The pass Statement

  

lest we first understand with use case diagram the how the loop works:

 




 

 

Lest we understanding the all of methods one by one in brief are mentioned here

1)     While Loop :

 

The while loop are used in conditional looping i.e when the condition are true then executed the set of statement are alog with condition are masoned

 

Lest we more getting clearance about the syntax of the while loop

 

Code:-

 

x = 1

while x < 5:

print (x)

x += 1




·         The break Statement:

 

·         The break statement ends the current loop and jumps to the statement immediately following the loop

·         It is like a loop test that can happen anywhere in the body of the loop

Code : -

 

while True:

    line = input('> ')

    if line == 'done' :

        break

    print(line)

print(“Done!”)

 

lest we understand with use case diagram how the code is work,

 



The continue Statement : -

 

With the continue statement we can stop the current iteration, and continue with the next: The continue statement ends the current iteration and jumps to the top of the loop and starts the next iteration

Code : -

 

while True:

    line = input('> ')

    if line[0] == '#' :

        continue

    if line == 'done' :

        break

    print(line)

print('Done!')

 

 

lest we understand the continue loops is work

 



·         The else Statement :  ( Repeated Steps )

 

With the else statement we can run a block of code once when the condition no longer is true:

Loops (repeated steps) have iteration variables that change each time through a loop.  Often these iteration variables go through a sequence of numbers.

 

Code :

 

n = 5

while n > 0 :

    print(n)

    n = n – 1

print('Techshoot03!')

print(n)

 

 

 

Indefinite Loops : -

 

         While loops are called “indefinite loops” because they keep going until  a logical condition becomes False

         The loops we have seen so far are pretty easy to examine to see if they will terminate or if they will be “infinite loops”

         Sometimes it is a little harder to be sure if a loop will terminate

 

 

 

         Quite often we have a list of items of the lines in a file - effectively a finite set of things

         We can write a loop to run the loop once for each of the items in a set using the Python for construct

         These loops are called “definite loops” because they execute an exact number of times

 

A Definite Loop with Strings: -

 

In the type of the program user the String value instant of any logic or any of characters ( Special character , numbers )

 

 

 

Code : -

 

frnd = ["encky""mincky""chincky"]
for in frnd:
  
print(x)

The range() Function :

To loop through a set of code a specified number of times, we can use the range() function,

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

Using the range() function:

Code: -

for x in range(9):
  print
(x)

 

The is and is not Operators

         Python has an is operator that can be used in logical expressions

         Implies “is the same as”

         Similar to, but stronger than ==

         is not  also is a logical operator

 

Code : -

smallest = None

print('Before')

for value in [13, 141, 112, 19, 714, 115] :

    if smallest is None :

        smallest = value

    elif value < smallest :

        smallest = value

    print(smallest, value)

print('After', smallest)