0% found this document useful (0 votes)
4 views6 pages

Python Codes

The document provides an overview of various Python programming concepts including input functions, string manipulation, conditional statements, lists, tuples, dictionaries, sets, loops, functions, and file I/O. It explains the characteristics of data types like mutable and immutable, as well as examples of how to implement these concepts in code. Additionally, it includes a simple calculator example demonstrating user input and basic arithmetic operations.

Uploaded by

shravan.hegde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Python Codes

The document provides an overview of various Python programming concepts including input functions, string manipulation, conditional statements, lists, tuples, dictionaries, sets, loops, functions, and file I/O. It explains the characteristics of data types like mutable and immutable, as well as examples of how to implement these concepts in code. Additionally, it includes a simple calculator example demonstrating user input and basic arithmetic operations.

Uploaded by

shravan.hegde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link] Function and use cases.

Note - Output of a input function is always string data type. Use type
casting wherever necessary.

Example - Marks = int(input("Enter Marks :"))

[Link] in Python - Getting the character of a str based on it's position.

=Str[0,1,2...]

3. Slicing in Python - Accessing a parts of a string

=str[startingIndex : endingIndex] # ending index is not included in output

[Link] String functions in Python. Strings are immutable.

\n - Next line

etc...

Conditional statements:

[Link]-elif-else and nested if

Syntax -

if(condition):

print("Output")

elif(condition):

print()

else:

print()
Lists and Tuples:

6 List is a built in Data type in Python. it's like an array which can store
elements of different Data type.

Lists are mutable.

EX - Marks = [50,100,46,...]

Mutating example:

Names = ["Sid","Mark","Frank"]

Names[0] = "John"

List also has some Methods/Functions. Unlike functions in string, functions in


lists don't return anything. They just make the change to list.

7. Tuple - Tuple is immutable.

Example - Tup = (1,2,3)

Dictionary and Sets

8. Dictionaries are used to store Data values in 'Key:Value' Pairs.

Dict = {"Key1" : "Value1",

"Key2" : "Value2"}
Nested Dictionaries are possible to create.

Methods in Dictionaries.

Ex -

[Link]() - Returns all Keys

[Link]() - Returns all values

[Link]("KeyName") - Returns Value for the Specified Key

[Link]({Key:Value,...})

...

[Link]

Set ignores duplicates

Ex - Set = {1,2,3,4}

Sets are immutable.

-LOOPS-

1. WHILE Loop:

Example:

tpl = (1,4,9,12,16,25,36,49)

n=10

i=0

while i < len(tpl):

if (tpl[i] == n):

print("Found at IDX:",i)

i+= 1
Break and Continue in Loops

Break Stops the Loop and

Continue Skips the next part of the Iteration and starts the next Iteration

2. For Loop

Ex: List = [1,2,3,4]

for Val in List:

print(Val)

Pass Statements

Range(Start, Stop, Increment_by)

Functions:

Ex-

def Odd_Eve(A):

if(A%2 == 0):

print("Even")

else:

print("Odd")

Odd_Eve(int(input("Enter a Number:")))

RECURSIONS:
******************************************

FILE Input/Output

Open/read/Close

File= open("[Link]", "r/w/a")

R-Read Mode

W- Write Mode - This rewrites the entire file

A - Append Mode - This appends the Text at the end

************************************

A = int(input("Enter first number:"))

B = int(input("Enter Second number:"))

D = "Choose 1 for Addition\nChoose 2 for Subtraction\nChoose 3 for


Multiplication\nChoose 4 for Division\n"

print(D)

C = int(input("Choose Between 1 to 4:"))

if C == 1:

print( A + B)

elif C == 2:

print( A - B)

elif C == 3:

print( A * B)

else:
print(A/B)

21308051002200404215

You might also like