[go: up one dir, main page]

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

Python

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)
4 views13 pages

Python

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/ 13

PYTHON

1. Shortcuts
print("Weclome to Hartron Advance Skill Center")

Weclome to Hartron Advance Skill Center

print(23+83)

106

2. To check system version


import sys
print(sys.version)

3.12.4 | packaged by Anaconda, Inc. | (main, Jun 18 2024, 15:03:56)


[MSC v.1929 64 bit (AMD64)]

3. keywords
keywords are reserved predefined word in python

# To check all available keyword in python


import keyword
print(keyword.kwlist)

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await',


'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except',
'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',
'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try',
'while', 'with', 'yield']

# To check total number of available keyword in pyhton


len(keyword.kwlist)

35

4. identifiers
In Python, identifiers are the names you give to things like:

variables

functions

classes

modules
objects

Basically, an identifier is just a name used to identify something in your Python program.

5. Comments
Comments are put for better code readability

There two types of comment

# This is single line comment

# This is a single-line comment


x = 10 # Assigning value 10 to variable x

print(x) # Printing the value of x

'''This is multi line comment'''


x = 20
print(x)

20

'''
This is a multi-line comment.
It uses triple single quotes.
Python will ignore this text.
'''
x = 20
print(x)

20

"""
This is another way
using triple double quotes
to write multi-line comments.
"""
y = 30
print(y)

30

6. variables
variable are reserved memory location where we store some value. it is create a moment we
assign it some value

y=10 # Y is storing a value 10


# here y is a calling valiable
print(y) #printing content of variable o/p 10

10

print("welome to the Harton Advance Skill Centre")

welome to the Harton Advance Skill Centre

7. Assignment
In Python, assignment is the process of storing a value in a variable. This is done using the
assignment operator, which is the equal sign (=).

#Single Assignment
name="Sujay"
city="Delhi"
Age=23
Subject="Science"

# Why this is called single assignment? ANS=> because each variable


is assigned a value separately, one by one.

#Multiple Assignment
name, city, Age, Subject = "Sujay", "Delhi", 23, "Science"
# Why this is called Multiple assignment? ANS=> becauseall variables
are assigned values in a single line

8. Statement
In Python, a statement is a single line of code (or instruction) that the Python interpreter can
execute.

Examples of Python Statements:


#1. Assignment Statement

x = 10

#(assigns value 10 to variable x)

#2. Print Statement

print("Hello, World!")

#(outputs text to the screen)

Hello, World!
#3. If Statement
if x > 5:
print("x is greater than 5")

#4. Loop Statement

for i in range(10):
print(i)

0
1
2
3
4
5
6
7
8
9

🔹 Types of Statements in Python


1. Simple Statements – written in one line (e.g., assignment, print).

2. Compound Statements – that control other statements (e.g., if, while, for,
function, class)

🔹 Data
Data means information that we store and process in a program.

Example: Numbers, text, lists, etc.

#👉 In Python, everything you create or store is some form of data.


x = 10 # data = number
y = "Sujay" # data = text
z = [1, 2, 3] # data = list

Data types
Basically there are five types of data in python

int,complex, float,bool,string
int
import sys
x=12
print(x)
print(type(x))
print(isinstance(x,int))
print(sys.getsizeof(x))

12
<class 'int'>
True
28

complex
import sys
x=12+10j
print(x)
print(type(x))
print(isinstance(x,int))
print(sys.getsizeof(x))

(12+10j)
<class 'complex'>
False
32

float
import sys
x=12.5
print(x)
print(type(x))
print(isinstance(x,float))
print(sys.getsizeof(x))

1654216154.5
<class 'float'>
True
24

bool
import sys
x=True
print(x)
print(type(x))
print(isinstance(x,bool))
print(sys.getsizeof(x))
True
<class 'bool'>
True
28

string
import sys
x="hi how are you"
print(x)
print(type(x))
print(isinstance(x,str))
print(sys.getsizeof(x))

hi how are you im fine


<class 'str'>
True
63

String Creation
str1="Hello python"
str1

'Hello python'

str2="hello world"
str2

'hello world'

string indexing
str1="Hello python"
str1

'Hello python'

str1[6]

'p'

str1[5]
#indexing is also count space and the index number of space is 5 thats
why here print ' '

' '

str1[7]

'y'

print(str1[6],str1[7])
p y

str1[-2]

'o'

str1[-8]

'o'

str1[-6]

'p'

str1[-9]

'l'

string slicing
str1

'Hello python'

str1[0:7] #it contains starting index 0 and ending index 7

'Hello p'

str1[5:10] #it contains starting index 5 and ending index 10

' pyth'

str1[-6:-2] #it contains starting index -6 and ending index -2

'pyth'

str1[:]

'Hello python'

str1[1:]

'ello python'

str1[:10]

'Hello pyth'

str1[-9:]

'lo python'

str1[:-9]
'Hel'

Used to reverse the string


str1[::-1]

'nohtyp olleH'

skip 1 chatacter
str1[0:12:2]

'Hlopto'

skip 2 chatacter
str1[0:12:3]

'Hlph'

update and delete string


string are immutable(unchangeable) that means we cannot delete any specific chafratcter string
of string

str1

'Hello python'

x=(str1.replace("He","Hi"))

'Hillo python'

str1

'Hello python'

del str1

str1

----------------------------------------------------------------------
-----
NameError Traceback (most recent call
last)
Cell In[73], line 1
----> 1 str1

NameError: name 'str1' is not defined


backup = str1
del str1
print(backup) # still accessible

Hello python

backup

'Hello python'

String concatenation
str1="Data"
str1

'Data'

str2="Science"
str2

'Science'

print(str1+str2)

DataScience

print(str1+' '+str2)

Data Science

String membership
str1="Science"
'S' in str1

True

'i' in str1

True

'k' in str1

False

'p' in str1

False

String partitioning
str1="Raju and Ramu and Neeta are best friends"
str1
'Raju and Ramu and Neeta are best friends'

str1.partitioning("and")

----------------------------------------------------------------------
-----
AttributeError Traceback (most recent call
last)
Cell In[94], line 1
----> 1 str1.partitioning("and")

AttributeError: 'str' object has no attribute 'partitioning'

str1.partition("and")

('Raju ', 'and', ' Ramu and Neeta are best friends')

str1.partition("are")

('Raju and Ramu and Neeta ', 'are', ' best friends')

Built in Functions string


str1

'Raju and Ramu and Neeta are best friends'

count
In Python, the count() method is used to count how many times a substring or element appears
in a string (or in a list).

str1.count('e')

str1.count('R')

strip
it will remove white space bydefoult from both side of string

str1=' hello '


str1

' hello '

str1.strip()

'hello'
rstrip
it will remove white space bydefoult from right side of string

str1.rstrip()

' hello'

lstrip
it will remove white space bydefoult from right side of string

str1.lstrip()

'hello '

str1="***Hello$$$"
str1

'***Hello$$$'

str1.strip("*$")

'Hello'

Upper & Lowwer


str1="hello python"

str1

'hello python'

str1.upper()

'HELLO PYTHON'

str2="GOOD DAY"
str2

'GOOD DAY'

str2.lower()

'good day'

Startwith()
Returns True if string start with arguments string

str1 = "hello world"


print(str1.startswith("h")) # True
print(str1.startswith("he")) # True
print(str1.startswith("w")) # False

True
True
False

Endswith()
Returns True if string end with arguments string

str1 = "Python is fun"

print(str1.endswith("fun")) # True
print(str1.endswith("n")) # True
print(str1.endswith("is")) # False
print(str1.endswith("Fun")) # False (case-sensitive)

True
True
False
False

Split()
in Python, the .split() method is used to break a string into a list of substrings based on a
separator (default is space " ").

text = "Python is very easy to learn"


result = text.split()
print(result)

['Python', 'is', 'very', 'easy', 'to', 'learn']

text = "apple,banana,grapes,mango"
result = text.split(",")
print(result)

['apple', 'banana', 'grapes', 'mango']

text = "one-two-three-four"
result = text.split("-")
print(result)

['one', 'two', 'three', 'four']

text = "I love Python programming"


result = text.split(" ", 2)
print(result)

['I', 'love', 'Python programming']


text = "I love Python programming language"
result = text.rsplit(" ", 2)
print(result)

['I love Python', 'programming', 'language']

Split by character
text = "Hello"
result = list(text) # OR text.split() won’t work for characters
print(result)

['H', 'e', 'l', 'l', 'o']

text = "banana"
result = text.split("a")
print(result)

['b', 'n', 'n', '']

You might also like