print("Hello World!") |
|
Example 1
#This is a comment |
|
|
Example 2
#This is a comment |
|
|
Example 3
""" This is a comment |
x = "How old are you ?" #x is of type str |
|
Text Type:
|
str |
|
Numeric Types:
|
int, float, complex |
|
Sequence Types:
|
list, tuple, range |
|
Mapping Type:
|
dict |
|
Set Types:
|
set, frozenset |
|
Boolean Type:
|
bool |
|
Binary Types:
|
bytes, bytearray, memoryview |
|
Get the data type of a variable "var"
|
|
type(var) |
|
|
Example
|
Data Type
|
x = "Color" |
list |
x = 1 |
int |
x = 1.2 |
float |
x = 2j |
complex |
x = ["Blue","Red","Yellow"] |
list |
x = ("Blue","Red","Yellow") |
tuple |
x = range(8) |
range |
x={"Age":25,"Height":1.72} |
dict |
x = {"Pink","Red"} |
set |
x = frozenset({"Pink","Red"}) |
frozenset |
x = True |
bool |
x = b"Color" |
bytes |
x = bytearray(8) |
bytearray |
x = memoryview(bytes(8)) |
memoryview |
|
Get the data type of x :
|
|
x = "Color" |
|
|
Casting is used to specify a type on to a variable and this is done using constructor functions.
Examples x = int(5) #x = 5 x = int(2.8) #x = 2 x = float(5) #x = 5.0 x = float(2.8) #x = 2.8 |
|
Operations on strings and examples:
|
|
|
Multiline strings
|
|
x = """ This is a multiline string""" |
|
|
Get the character at a specific position
|
|
x = "Python Programming" |
|
|
Slicing
|
|
x = "Python Programming" |
|
|
Negative Indexing
|
|
x = "Python Programming" |
|
|
String Length
|
|
x = "Hello" |
|
|
Remove any whitespace from the beginning or the end
|
|
x = " Hello " |
|
|
Return the string in lower case
|
|
x = Hello |
|
Return the string in upper case
|
|
x = Hello |
|
|
Replace a string with another string
|
|
x = "Hello" |
|
|
Choose a separator and split string into substrings
|
|
x = "Python Programming" |
|
|
Check if a string is present in a text
|
|
txt = "Tunisia is a North African country" |
|
|
Concatenation
|
|
x = "Hello" |
|
|
Insert numbers into strings
|
|
quantity = 3 |
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.