Posts

Showing posts from March, 2025

String Slicing and Operations on Strings

names = "Sindhu , Shilpa" print(names[0:6])      #output = Sindhu #print(names[1st index:(n-1)th index]) where (n-1)=5 ''' In Programming, counting always starts with 0. Length of a String: We can find the length of a string using len() function. ''' print(len(names))#output = 15 fruit = "Orange" len1 = len(fruit) print("Orange is a" ,len1, "letter word.") #output = Orange is a 6 letter word. '''For functions, we use parenthesis and for string slicing, we use square brackets.''' alphabets = "ABCDE" alphabetslen = len(alphabets) print(alphabetslen)      #output = 5 print(alphabets[0:4])    #output = ABCD print(alphabets[ :4])    #output = ABCD # In case, we do not put any index at the place of 1st index, Python considers it as 0 by default. print(alphabets[1:4])    #output = BCD #print(alphabets[1st index:3rd index]) i.e. including 1 but not 4 print(alphabets[:])     #output = ABCDE #In cas...

Strings

''' In python, anything that is enclosed between a single or double quotation marks is considered as a string. A string is generally a sequence or array of textual data. It does not matter whether we enclose our strings between single or double quotes, the output remains same. If there are some textual data that are enclosed within triple single quotes or triple double quotes, that is also considered as a string. To convert a single line into a string, we can use either single quotes or double quotes or triple single quotes or tiple double quotes. To convert multiple lines into a string, we can only use either triple single quotes or triple double quotes. ''' name = "Sindhu" friend = 'Rohan' print("Hello, "+ name)      #output = Hello, Sindhu #Plus is used to connect the strings as they are. #apple = "He said, "I want to eat an apple.""   #output = error # We cannot put double quotes inside double quotes. apple = ...

Round Function

  '''The round() function in Python is used to round a number to a specified number of decimal places. round(number, ndigits)''' number=float(input("Enter a number: ")) ndigits=int(input("Enter the number of decimal places to round to: ")) print(round(number, ndigits)) # The first variable is the number to be rounded, and the second variable is the number of decimal places to round to. print(round(3.14159, 2))   # Output: 3.14 print(round(5.678, 1))     # Output: 5.7 print(round(4.5))          # Output: 4 (rounds to the nearest even number)  print(round(5.5))          # Output: 6 (rounds to the nearest even number) # If ndigits is not specified, the default value is 0, which means rounding to the nearest integer. print(round(3.14159))      # Output: 3 # If ndigits is negative, it rounds to nearest 10s,100s, etc. as base 10 numbers. print(round(5.678, -1))    # Output: 10.0 print...

User Input in Python

 ''' User Input in Python In Python, we can take user input directly by using input() function. variable=input() But input function returns the value as a string only. So we have to typecast them whenever requires to another datatype. ''' a = input("Enter your name: " ,) print("My name is", a) #output= My name is Sindhu '''While writing an input statement, we need to have some space for the data to be stored for the user to input. After using a string statement to give instruction, to the user to make sure to have a comma after the string statement.''' x = input("Enter first number: " ,) y = input("Ener second number: " ,) print(int(x) + int(y)) #output=33

Typecasting in Python

 ''' Typecasting in Python: The conversion of data types in Python is known as type casting. Python supports a wide variety of functions or methods like: int(), float(), str(), ord(), hex(), oct(), tuple(), set(), list(), dict() etc. for the type casting in Python. ''' a="1" b="2" print(a+b)        #output=12 # We can see the output is 12 because the value of a and b are strings. print(int(a)+int(b))  #output=3 #We can see the output is 3 because now the values of a and b has become integer functions.  '''Either two integer functions or two string functions or any two functions of the same data type can be operated to get the result.''' ''' Two types of typecasting: 1. Explicit Conversion (Explicit typecasting in Python) 2. Implicit Conversion (Implicit typecasting in Python) Explicit Typecasting: The conversion of one data type into another data type, done via developer or programmer's intervention or ...

Arithmetic Operators

 ''' Python has different types of operators. To create a calculator we require arithmetic operators. Operator        Operator Name                     Example +                Addition                                      24+9 -                Subtraction                                  24-9 *                Multiplication                             24*9 /                Division                                  ...

Variables and Data Types

 '''Variable is like a container that holds data. Very similar to how our containers in kitchen holds sugar, salt etc. Creating a variable is like creating a placeholder in memory and assigning it some value.''' '''In Python, it is as easy as writing: a = 1 b = True c = "Sindhu" d = None These are four variables of different data types.''' a = 1 print(a)                          #output=1 b = True print(b)                          #output=True c = "Sindhu" print(c)                          #output=Sindhu d = None print(d)                          #output=None '''What is data type? Data type specifies the type of value a variale holds. This is required in programming to do various operations without causing an error.''' print(t...

Comments and Escaping Sequence

 #To insert any character that cannot be added into the print function directly, we use escape sequence character "\" print("Hey I am Sindhu\nand I am a good girl")   #output- Hey I am Sindhu                                                                                                  and I am a good girl # \n is an escape sequence character which is used to change the line. No need for a space after \n print("She said, \"Python is awesome!"")    #Double quotes inside the string output = She said, "Python is awesome!" #We use escape sequence character to print double quotes inside print () function which cannot be used directly. ''' If we use # in front of a line then it will be a comment and it will not be executed ct...

Print Function

#To print something in Python, we write print( ) #print is a function to perform any task. print("Hello World")                                    #output - Hello World #We use double quotes " " to make the object valid and to make it a string. #We can also use single quotes ' ' for the same. print('Hello World')                                      #output = Hello World print(5)                                                         #output - 5 #We didn't use double quote or single quote here as 5 is a number(a valid object) #To print multiple lines in print function, we use triple single or double quotes. print('''Hi How are you? I am good, Thank you''' print(...