0% found this document useful (0 votes)
46 views

My Python

The document provides an introduction to the Python programming language. It discusses that Python is a high-level programming language that can be used to create GUIs, web applications, games and more. It is an open source language. The document then covers various Python concepts like data types, operators, conditional statements, functions, loops and dictionaries. It provides examples to explain concepts like strings, lists, tuples, input functions, if-else statements and more.

Uploaded by

b d
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

My Python

The document provides an introduction to the Python programming language. It discusses that Python is a high-level programming language that can be used to create GUIs, web applications, games and more. It is an open source language. The document then covers various Python concepts like data types, operators, conditional statements, functions, loops and dictionaries. It provides examples to explain concepts like strings, lists, tuples, input functions, if-else statements and more.

Uploaded by

b d
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Python- a basic introduction

 Python is a high level programming language


 A compiler or interpreter has the function to convert high level language to low
level language
 High level language-human language (takes longer to be interpreted)
 Low level language -binary language (easily comprehendible by the computer)
 Difference between compiler and interpreter
 Python can be used for forming GUIs, web frames, , games etc.
 Python is open source- which means that it can be downloaded by anyone
without obtaining a specific license.

Macro can be defined as an automation wherein events that have been sequenced in a
particular order are executed automatically.

Cross plat forming-the integration of two languages in order to ease out the process to
attain a particular objective, like java+ python= jython

Consoles are separate windows for various purposes that appear upon execution of a
particular command. ‘

Stand- alone applications are the ones which do not require any other application to
function.

Embedded scripting- when a particular statement leads or triggers to the execution of


the other statement or program.

Logical operators- the operators which connect two statements…like- and, or etc.

Comments in python
-comments describe a particular code written. These are only displayed in the code
window for the convenience of the script reader. They do not get printed or executed.

-use of # for single line comment

-use of ‘’‘….’’’ or “”“…..””” For multiple line comments

-for example-

#this is my first python project

Or
‘’’hi

This
Is my

First project’’’

Or
“””hi

This is

Beautiful”””

Data types
1. Int(whole numbers)
2. Float (decimals)
3. Longint(long whole number)
4. String
5. Tuple
6. List

Assignment statement
This is in order to assign or store value in a variable.

Variable=expression

I.e. age-=20 , where = is assignment operator

Shifting to a new line or giving a tab space


Print("namrata is good \t she is pretty")
Print("namrata is good \n she is pretty")

Type

To determine the data type of a variable (which python recognizes itself)


Use function.
X=10
y='namrata'
z='pearl1234'
a=12.00
print(type(x),type(y), type(z) , type(a))

Result-
<class 'int'> <class 'str'> <class 'str'> <class 'float'>

Swapping of variables
X=10
y=15
x,y=y,x
print (x,y)
XXXXX
Operations that can be performed on numeric variables-
Multiplication (*)
Addition (+)
Subtraction (-)
Division (/)
Floor division (doesn’t show the decimal part)(//)
Exponentiation (**)
Modulus (shows remainder)(%)

Boolean data type- true or false

Print(x==y) (equal to)(note- double equals tosign)


Print(x<y) (less than)
Print(x<=y) (less than, equal to)
Print(x>y) (greater than)
Print (x>=y) (greater than, equal to)
Print(x!=y) (not equal to)

Operations on string

1. Index (individual extraction)


X=’namrata’
Print(x[0])
(in order to print the specefic character of a particular string result
will be n here)

2. Slicing ( to display a particular range out of a particular


string)
X=’namrata’
Print(x [0:7]) --- letters from 0 to 6 appear
Print (x [:9]) ---- letters from first to 8th

3. Count
X=’namrata’
Print (x.count (‘a’))

Note-
-in order to count the number of as in namrata
-we can do the same with numbers by converting them into
string by putting single quotes.

4. Find
x= ‘namratamunjal’
Print (x.find (‘a’, 2)

The answer displayed will be 4

.. X=’assassination’
Print (x.find (‘s’, 4)

The answer will be 4

Note-
-to find the position of a specified character
-this can be done by specifying the number of characters after
which the desired letter has to be counted.

5. Case change
name='ramanujan saarvey'
print(name.upper())

name='ramanujan'
print(name.lower())

name='ramanujan'
print(name.capitalize())-----(convert the first
character to capital)

name='ramanujan'
print(name.title())-------(convert the first
character of the words present to capital)

name='ramanujan'
print(name.swapcase())------(toggle case)

6. Strip method
In order to remove the unwanted character from
the extreme left or extreme right or both.
name='ramanujan'
print(name.rstrip("n"))

name='ramanujan'
print(name.lstrip("r"))

(To remove characters present on bith right and


left side-)
name='*ramanujan*'
print(name.strip("*"))

7. Split method- To dis[play the characters of


the string upon mentioning the separator

name='1-2-3-4-5-6-7'
name1=(name.split("-"))

8. Join
name='1-2-3-4-5-6-7'
name1=(name.split("-"))
print(':'.join(name1))

9. Replace
name='1-2-3-4-5-6-7'
print(name.replace('7','8'))

10.Justification
name='himani'
print(name.rjust(10,'*'))

name='himani'
print(name.ljust(10,'*'))

name='himani'
print(name.center(10,'*'))

11.Zfill (in order to add zeroes to complete the


mentioned number of characters)

name='himani'
print(name.zfill(10))

12. Percentage spacing


Print("%13s%25s%6s"%("name" ,"portfolio" , "marks"))
print("%13s%25s%6d"%("naina", "iceland", 95))
print("%13s%25s%6d"%("maina kumari" ,"congo" ,100))
print("%13s%25s%6d"%("sunaina" ,"united states of america" ,98))

13. Adding of strings


print('namrata'+'munjal')

to print twice the variable salary


x=400
print('Salary is',x*2)

Note-
Use comma to define the use of existing variable

IF AND ELSE STATEMENTS


Example 1-
smith-20 years- $60000 pa graduate
sella- 20 years- $80000 pa graduate

who is performing better in terms of money?

name='smith'
name2='cella'
age=20
if name=='smith'and age==20:
print('Salary is $60000')
else :
print('salary is $80000 ')

Example 2-

is_criminalhistrory=False
if is_criminalhistrory:
print('yes')
else:
print('no criminal')

Example 3-
no_criminal_history=True
good_credit_history=False
principal_amount=1000000
time=10
x=8
y=12
if no_criminal_history and good_credit_history:
print('EMI=',(principal_amount*time*x)/100)
else:
print('EMI=',(principal_amount*time*y)/100)

no_criminal_history=True
good_credit_history=False
principal_amount=1000000
time=10
x=8
y=12
if no_criminal_history and
good_credit_history:#THIS MEANS IF BOTH CONDITIONS
ARE TRUE.
print('SI=',(principal_amount*time*x)/100)
else:
print('SI=',(principal_amount*time*y)/100)

x=5
if x>0:
print('x is postive')
elif x==0:
print('x is equal to 0')
else:
print('x is negative')

NOTE- USE OF DOUBLE == SIGN TO ASSIGN VALUE

INPUT FUNCTION

Input function has default string data type. We need to


convert into integer
english=(int(input('enter the marks in
english!')))
maths=(int(input('enter the marks in maths!')))
science=(int(input('enter the marks in science')))
hindi=(int(input('enter the marks in hindi')))
average_marks=(english+maths+science+hindi)/4
if average_marks>=95:
print(' oustanding result')
elif average_marks<95 and average_marks>=90:
print('very good result')
elif average_marks<90 and average_marks>=80:
print('good result')
elif average_marks<80 and average_marks>=60:
print('average result')
else: print('bad result')

salary_employee1=(int(input('enter the salary of


the first employee')))
salary_employee2=(int(input('enter the salary of
second employee')))
salary_employee3=(int(input('enter the salary of
third employee')))
salary_employee4=(int(input('enter the salary of
fourth employee')))
average_salary=
(salary_employee1+salary_employee2+salary_employee
3+salary_employee4)/4
if average_salary>=700:
print('the employee is well paid.')
elif average_salary<1000 and average_salary>=500:
print('the employee is moderately paid.')
else: print('the employee is not paid according to
the decided norms')
]
What if you want to specify the length of the input to be entered
DICTIONARY

Dictionary is a set of a stored value along with related features or


words.

dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(dict1['red'])

‘red’:’lal’- item/ key value pair

FUNTIONS
dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(len(dict1))

dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(max(dict1))

dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(min(dict1))

dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(list(dict1))

METHODS

COPY METHOD
dict1={'red':'lal','green':'hara','orange':'narang
i'}
d2=dict1.copy()
print(d2)
GET METHOD- IN ORDER TO FIND OUT THE ASSOCIATED
WORD OF A SPECIFIED WORD FROM THE DICTIONARY.
dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(dict1.get('green'))

dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(dict1.keys())

dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(dict1.values())

dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(dict1.items())

dict1={'red':'lal','green':'hara','orange':'narang
i'}
dict2={'red':'LAL'}
dict1.update(dict2)
print(dict1)

dict1={'red':'lal','green':'hara','orange':'narang
i'}
dict1.clear()
print(dict1)
Loops
TUPPLE- storing of multiple values in one variable.
PRIME_NUM=(2,3,5,7,11,13,17,19)
print(type(PRIME_NUM))

LIST
PRIME_NUM=[2,3,5,7,11,13,17,19]
print(type(PRIME_NUM)))

The difference between list and tuple is that the values stored in a variable
of tuple data type can’t be changed.

In order to find the number of characters in a


given tuple
PRIME_NUM=(2,3,5,7,11,13,17,19)
print(len(PRIME_NUM))

PRIME_NUM=(2,3,5,7,11,13,17,19)
print(max(PRIME_NUM))

PRIME_NUM=(2,3,5,7,11,13,17,19)
print(min(PRIME_NUM))

In order to determine the number on a particular


position
PRIME_NUM=(2,3,5,7,11,13,17,19)
print(PRIME_NUM[0])

INDEXING- In order to display a range of values


PRIME_NUM=(2,3,5,7,11,13,17,19)
print(PRIME_NUM[0:7])
PRIME_NUM=(2,3,5,7,11,13,17,19)
print(PRIME_NUM[0:])

PRIME_NUM=(2,3,5,7,11,13,17,19)
print(PRIME_NUM[:])

tupl=('namrata','hardik','pearl')
x,y,z=tupl
print(x)

tupl1=('namrata','hardik','pearl')
tupl2=('riya','siya','kunal')
print(tupl1+tupl2 )

veg=('tomato','spinach','ladyfinger')
fruit=('mango','apple','melon')
p,q,r,s,t,u= veg+fruit
print(t)

ALL THE METHODS ARE SAME FOR LIST AND TUPLE EXCEPT
THAT LIST CAN BE UPDATED.

veg=['tomato','spinach','ladyfinger']
fruit=['mango','apple','melon']
veg[0]='potato'
print(veg)

veg=['tomato','spinach','ladyfinger']
fruit=['mango','apple','melon']
del veg[0]
print(veg)

veg=['tomato','spinach','ladyfinger']
fruit=['mango','apple','melon']
if 'broccoli'in veg:
print('match available')
else: print('match unavailable')
Conversion of list to tuple

x=(1,2,3,4,5,6)
list1=list(x)
print(type(list1))

x=[1,2,3,4,5,6]
tupl=tuple(x)
print(type(tupl))

x=[1,2,13,64,15,6]
print(sorted(x))

You might also like