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

Python - Basics

The document provides an overview of Python basics, focusing on strings, including their representation, Unicode, escape sequences, raw strings, and formatting. It also covers string slicing, indentation, and includes examples of Python code for calculating the sum of digits and checking for Armstrong numbers. The content is structured for educational purposes, likely aimed at students learning Python programming.

Uploaded by

Ganga devi D.
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 views10 pages

Python - Basics

The document provides an overview of Python basics, focusing on strings, including their representation, Unicode, escape sequences, raw strings, and formatting. It also covers string slicing, indentation, and includes examples of Python code for calculating the sum of digits and checking for Armstrong numbers. The content is structured for educational purposes, likely aimed at students learning Python programming.

Uploaded by

Ganga devi D.
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

22/09/2025

PYTHON BASICS

[Link] devi,MCA.,[Link].,SET.,NET.,
Head & Asst. Professor,
Dept. of Data Science,
SFRC.

08.07.2024

STRINGS

• Group of characters.
• Can display strings in three ways:
 Single Quotes
 Double Quotes
 Triple Quotes

1
22/09/2025

STRING

UNICODE

• It’s a standard way of writing international text. i.e in your native language.
• Simply include a ‘u’ or ‘U’ in front of text.

2
22/09/2025

ESCAPE SEQUENCE

• Some characters cant be included in a string directly.


• The characters must be escaped by a backslash before them.
• A escape sequence is a combination of characters i.e. translated into
another character which may be difficult to represent directly.

3
22/09/2025

RAW STRINGS

• Specify a string as a raw string,


 To specify a string that should not handle any escape sequences.
 To display exactly as specified
• To specify raw string, prefix it with ‘r’ or ‘R’

STRING FORMATTING

• format() function is used to control the display of Strings.


• Syntax:
format(value, format_specifier)
• Where,
value – string to be displayed
format_specifier – combination of formating options.

4
22/09/2025

ALIGNMENT

 < - Left Justify


 > - Right Justify
 ^ - Central Alignment

FILL THE WIDTH

• In Default, blank spaces are used to fill the specified width.


• Can fill the width in the formatted string using specified character as
follows:

5
22/09/2025

STRING SLICING

• The substring of a string is called a Slice.


• Can extract the subsets of strings using the slice operator ([] and [:]).
• Need to specify the index of characters to be extracted.
For extracting characters form the beginning,
• The index of first character is 0, where the last character is n-1. (where n is the number of
characters)
For extracting characters form the end of the string,
• The index of the last character is -1, whereas the index gets reduced to the beginning.

STRING SLICING

• The Syntax is
Str_vble[start:end:stride]
• Where,
start – beginning index of the substring
end – index of the last character
stride – number of characters to be extracted
• Omitting both may extract the whole string.

6
22/09/2025

PRINTING MULTIPLE TIMES

7
22/09/2025

INDENTATION

• Whitespace or the tabs at the beginning of the logical line is called


indentation.
• It determines the indentation level of the logical line.
• In other languages, indentation has no effect on the programming logic. In
python, indentation is used to associate and group statements.
• In python, the statements in a block must have the same indentation level.
• It strictly checks the indentation level and gives an error if indentation is
not correct.

• The ^ is a standard symbol that indicates that an error has occurred.


• Python doesn’t use {…..} to indicate blocks of code. It uses only
indentation to form a block.

8
22/09/2025

# To find Sum of Digits


number = int(input("Enter the number: "))
sum = 0
while number > 0:
LastDig = number % 10
number = number // 10

sum = sum + LastDig


print("The Sum of the digits : " ,sum)

# To find Sum of Digits Number = 4545,


number = int(input("Enter the number: ")) Result = 18
Number LastDig Sum
sum = 0
while number > 0: 4545 5 0+5
LastDig = number % 10 454 4 0+5+4
number = number // 10
45 5 0+5+4+5
sum = sum + LastDig 4 4 0+5+4+5 + 4
print("The Sum of the digits : " ,sum)
0 Exit the Loop

9
22/09/2025

ARMSTRONG NUMBER
CHECKING

number = int(input("Enter a number: "))


num = number
143 = 13+43+33
digit, armstrong_sum = 0, 0 = 1 + 64 + 27
while num>0: = 92
digit = num % 10 153 = 13+53+33
num = num // 10 = 1 + 125+ 27
= 153
armstrong_sum += pow(digit, 3)

if armstrong_sum == number:
print("It's an Armstrong number:", number)
else:
print("Not an Armstrong number:", number)

10

You might also like