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

Python

The document outlines data type conversions in Python, specifying which conversions are possible and which are not. It also covers various printing formats, operations on variables, string manipulation methods, and data structures like lists, tuples, sets, and dictionaries. Additionally, it details methods for manipulating lists and tuples, including adding, removing, and sorting elements.

Uploaded by

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

Python

The document outlines data type conversions in Python, specifying which conversions are possible and which are not. It also covers various printing formats, operations on variables, string manipulation methods, and data structures like lists, tuples, sets, and dictionaries. Additionally, it details methods for manipulating lists and tuples, including adding, removing, and sorting elements.

Uploaded by

irisnadi1624
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Pyth on

By Phway
Data type changing (can or can’t)

Can Can’t
Float to int
String to int (numbers) String to int (words)
Bool to int Complex to int
Float to int None to int
String to float (numbers) String to float (words)
Int/float to complex string to complex (words)
String to complex (numbers) None to complex
To string/bool – everything
Bool to complex
printing format
Normal = print(“……………{var}……………….{var}…………….”)
F = print(f“………………..{var},,,,,,,,,,{var}”)
% = print(“………………..%…………………..%,,,,,,,,,,,,,,,,%”.%(var,var,var))
.format = print(“………………..{}…………………..{},,,,,,,,,,,,,,,,
{}.format(var,var,var))
Operat ion
Update
var operation = number
+ (Add)
Example
- (Sub)
a = 4/ b = 3
* (Multiple)
a -=3 (1)
/ (Division with float)
a +=3 (7)
// (Division with integer)
b*=3 (9)
% (Rem)
b /=3 (0)
** (powder )
a *=1 (4)
Name Formula e Example
Printing a print(var[position:]) print(a[13:])
Substring
Removing
Whitespace From .strip()
Strings

Changing Letter .upper()


Cases in Python .lower()
.title()
Name Formula e Example
Finding the Length len(var) print(len(a))
of the String
Combining or
print( String1 +
Concatenating More var+var
String 2)
Than One String

Adding Newlines in
Python \n
Name Formula e Example
\t
Tabs

Add comment #

Multiple lines \
Name Formula e Example
var[position]
index

Add comment #

Multiple lines \
Keeping a group of data
List [ ]
Tuple ( )
Set { }
Dictationary { }
Element

List = [a,b,c,d,e]

Element
(no need to be the same data type)
Dictation ary

List = { “Mary” : 80, “John” ; 30}

Key Name
(must be sting)
Repeat Order Index Mute

List

Tuple

Set

Dictationary
List
Index - var[position]
mute - var[position] = sth u wanna replace
list to tuple or set
var = tuple(var)
var = set(var)
Tuple
Index - var[position]
tuple to list or set
var = list(var)
var = set(var)
Set
set to list or tuple
var = list(var)
var =tuple(var)
Dictio nary
Index - var[position]
mute - var[position] = sth u wanna replace
L = [ - 2 , 0 , 9 , 5 , - 3 ]
wanna print a
group ?

var = [ start : stop + 1: step ]


wanna print a
group ?
Reversed
var = [ start : stop - 1: - step ]
Compou nd data Structu re
list of list
list of tuple
Metho d_List
append(item): Adds an item to the end of the list
extend([ , , , , , ]): Appends the elements of an iterable (e.g., another list)
to the end of the list
insert(index, item): Inserts an item at a specific index in
the list.
Metho d_List
remove(item): Removes the first occurrence of the
specified item from the list.
pop(index): Removes and returns the item at the
specified index. If no index is provided, it removes the
last item.
Metho d_List
index(item): Returns the index of the first occurrence of
the specified item.
count(item): Returns the number of times the specified
item appears in the list.
Metho d_List
sort(): Sorts the list in ascending order. (in original one)
sorted : not original one
reverse(): Reverses the order of elements in the list.
Metho d_Tup le
count(item): Returns the number of times a specified item appears in the
tuple.
index(item): Returns the index of the first occurrence of a specified item.
sorted(iterable): Returns a new sorted list from the elements of the
tuple.
Metho d_Tup le
min(tuple): Returns the minimum value in the tuple.
max(tuple): Returns the maximum value in the tuple.
sum(iterable): Returns the sum of all elements in the tuple.
len(tuple): Returns the number of items in the tuple.
\ list
A=[1, 2, 3, 5, 9, 10]
print(min(A))
print(max(A))
print(sum(A))
print(len(A))
Than k you !

You might also like