0% found this document useful (0 votes)
0 views25 pages

Python 2

The document provides an overview of Python programming basics, covering comments, pseudocode, flowcharts, bugs, variables, functions, and data types. It explains the importance of comments for code readability and outlines different types of functions and data types available in Python. Additionally, it includes examples of string manipulation techniques such as indexing, slicing, replacing, and splitting strings.

Uploaded by

Sonu Shaukeen
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)
0 views25 pages

Python 2

The document provides an overview of Python programming basics, covering comments, pseudocode, flowcharts, bugs, variables, functions, and data types. It explains the importance of comments for code readability and outlines different types of functions and data types available in Python. Additionally, it includes examples of string manipulation techniques such as indexing, slicing, replacing, and splitting strings.

Uploaded by

Sonu Shaukeen
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
You are on page 1/ 25

Basics of Python

Comments

Lines of code ignored by the Python


interpreter.
Types of Comments
Single-line Multiline
(Docstring / String Literals)
Advantages of Comments

• Code Readability
• Explanation of the code
• Prevent execution of code
• To include resources
Pseudocode

Informal way of describing the steps of an


Algorithm.

Original code
Pseudocode
a = int(input(“Enter a number: ”))
1. Start
if a % 2 == 0:
2. Take an integer input
print(“even”)
3. Check if the number is divisible by 2
else:
4. If ‘yes’ print ‘Even’ else print ‘odd’
print(“odd”)
5. Exit
Flowchart
Graphical way of describing the steps of an
Algorithm. Start

input a
Original code

a = int(input(“Enter a number: ”)) a%2==


if a % 2 == 0: 0
print(“even”)
else:
“Even” “Odd”
print(“odd”)

Exit
Bugs
Any mistakes or syntax errors that violate the rules of
the language are called bugs.

For example:

a = int(input(“Enter a number: ”))


if a % 2 == 0:
print(“even”
else:
print(“odd”)
Variables

Variables are just containers or locations in


the memory that store some value.

use the id() function to get the memory address of the


variable.
Functions
Functions are reusable blocks of code that
perform a specific task.

Syntax:
print (arg1, arg2, …., end='\n', sep=‘ ‘)
For example:
print (“Hello Python”)
Types of Function
Built-in Functions:
• Input/Output Functions: print(), input()
• Type Conversion: int(), float(), str()
• String Function: replace(), split(), upper(), substring()
• Mathematical Function: pow(), fabs(), round(), sqrt()
• File handling Functions: open(), close(), read(), write()
User-defined Functions:
• Lambda function
• Recursive Function

https://docs.python.org/3/library/functions.htm
Data Types

The data type defines the type of data a variable


can hold, and the operation that can be
performed on that data.

Use the type() function to check the data type of any variable.
Data Types

Boolean Numeric Set Type Mapping Sequence String


Type

Integer Float Frozenset Set


Tuple List

Complex Dictionary
Numbers
Standard Built-in Data Types
Numeric types

• int()

• float()

• Boolean

• complex()
Integer types

It represents the whole numbers (positive, negative, or


zero) without decimals. (e.g., 1, -10)
Boolean types

It returns the truth value i.e. either ‘True’ (1) or ‘False’ (0).
Float types

Represent numbers with decimal points. (e.g. 3.14, -9.2 )


Python has 15-digit decimal precision.
Complex types

Represents real and imaginary numbers. (e.g. 1 + 2j, 2j)

Syntax: (real path) + (imaginary part )j

For example:
String Types

Represent sequences of characters. (e.g. "Hello", "This is a string")

Text(s) enclosed within single (‘ ’), double (“ ”), or triple(‘‘‘ ’’’) quotes
are string.

For example:
Index

The index is the positional value of elements in the sequence.


➔ Starts from ‘0’ to length-1 or -1 to -length

For example:
[“Welcome”, “to”, “python”, “programming”]
Length

Return the size of the String.

Syntax: len(<sequence_types>)

For example:
a = “hello world”
print(len(a))
Accessing characters
Syntax: <string>[index]

For example:
a = “hello world”
print(a[2])
Slicing String
Represent the part of the original string.

Syntax: <string>[start:end:step]

For example:
greeting = "Hello, world!"
sub_string = greetings[7:12]
Replace String

Replaces a substring with another substring in the original string..

Syntax: replace(<substring_to_replaced>,<substring_to_replace>)

For example:
greeting = "Hello, world!“
greetings.replace(“world”, “python”)
print(greetings)
Split String
Split the string into a list of tokens.

Syntax: split(<delimiter>)

For example:
greeting = “Welcome to the Python Programming“
print(greetings.split())

https://docs.python.org/3/library/string.html

You might also like