Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Python Notes

The document provides an overview of Python programming concepts, including variable definitions, data types, and built-in data structures like lists and tuples. It outlines operations such as arithmetic, comparison, and logical operations, along with methods for manipulating strings and lists. Additionally, it explains how to define global variables and includes examples of various operations and functions in Python.

Uploaded by

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

Python Notes

The document provides an overview of Python programming concepts, including variable definitions, data types, and built-in data structures like lists and tuples. It outlines operations such as arithmetic, comparison, and logical operations, along with methods for manipulating strings and lists. Additionally, it explains how to define global variables and includes examples of various operations and functions in Python.

Uploaded by

040601is
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Output Text Print(“abc”)

Print(x)
Print(X, Y, Z)
Print(1 + 2)
Define variable X=5
Y = “abc”
Define variable with X = str()
data type & length Y = int()
Z = float()
Define multi X, Y, Z = “a”, “b”, “c”
variables X = Y = Z = “a”
Add comment - #abc
- “””string
String”””
Get & print data Print(type(x))
type
Global variable 1. Define global variable normally
X = “abc” [Outside the def]

2. Define global variable within def


Def myfunc()
Global X

Variable

Data Type
Type Example
Str X = “Hello”
Int X = 20
Float X = 20.4
List X = [“a”, “b”, “c”]
Bool X = True
Random int Import random
Print(random.randrange(start, end))
Change data type X = int(2.6)
X = float(1)
X = str(2)
Data type - String
Return character by A = “Hello World”
index Print(a[4])
**o
Print character in For n in “apple”
variable one by one Print(x)
Check length of Print(len(x))
string
Check the variable Txt = “The best thing in life”
contains string Print(“life” in txt)
**True
Return strings by A = “Hello World”
index Print(a[1:4])
**ell

Print(a[:5])
**Hello

Print(a[2:])
**llo, World
Change to upper Print(a.upper())
case
Change to lower Print(a.lower())
case
Remove space Print(a.strip())
before & after string
Replace a character Print(a.replace)”a”, “b”)
with special case
Split string Print(a.split(“,”))
Formatting Print(f(“abc {int:. 2f} abc”))
Escape formatting \n
\t
Calculation Operator
Operator Assignment operator Meaning
+ += Addition
- -= Subtraction
* *= Multiplication
/ /= Division
% %= Get modulus
// //= Floor division

Comparison Operation
Operator Meaning
== Equal
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Logical Operator
Operator Short form Meaning
And & Both are reached
Or | Reach either one of it
Not ~ Inverts

Difference in 4 built-in data type


# Data type Ordered Changeable Duplicate
1 List ✓ ✓ ✓
2 Tuple ✓ ✓
3 Set
4 Dictionary ✓ ✓
Built-in Data Type – List
Item Meaning/ Example
Define myList = [“apple”, “orange”, “banana”]
myList = [1, 2, 3]
Check no. of item Print(len(myList))
Locate item myList = [“apple”, “orange”, “banana”, “cherry”, “mango”]

print(myList[1])
**orange

Print(myList[-1])
**mango

Print(myList[1:4])
**[“orange”, “banana”, “cherry”]

Print(myList[:3])
**[“apple”, “orange”, “banana”]

Print(myList[2:])
**[“banana”, “cherry”, “mango”]
Check a string in the list If “apple” in myList:
Print(“Yes”)
Change list item with myList[2] = “apple”
index (1-to-1)
Change list item (add) myList = [“apple”, “banana”, “cherry”]

myList[1:2] = [“banana”, “orange”] <<<>>> myList.insert(2, “orange”)


Change list item myList = [“apple”, “banana”, “cherry”]
(remove)
myList[0:1] = “” <<<>>> myList[0:2] = “apple”
Append item at the end myList.append(“apple”)
of list
Merge list (combine 2 list myList1.extend(myList2)
OR append multi items)
Remove item by string myList.remove(“apple”)
Remove item by index myList.pop(1)
Remove item at the end myList.pop()
of the list
Clear items in the list myList.clear()
Delete list myList.del()
Loop for list For x in myList:
Print(x)
Loop for list by index For i in range(len(myList)):
Print(myList[i])
Loop for list by while i=0
while i < len(myList):
print(myList[i])
i += 1
Look up value in list For x in myList:
If “a” in x:
Print(“Yes”)
Sort a list myList.sort()
Sort a list in descending myList.sort(reverse = True)
order (integer)
Sort a list in reverse myList.reverse()
order (String)
Copy a list to list (change newList = oldList.copy() <<<>>> newList = list(oldList) <<<>>> newList
not affect other) = oldList[:]
Link 2 lists newList = oldList
Combine lists List3 = list1 + list2
Built-in Data Type – Tuple
Define myTuple = (“apple”, “orange”, “banana”)

You might also like