The World Knows What's First - It's Everything in one
The World Knows What's First - It's Everything in one
you just no need to take care of stupid braces meanwhile your IDE will take care of your indents
Simple Hello world Execution
------------------------------------
Type Python in Console to execute the code,
example 1:
Python >>>
>> print ("Hello")
Hello
example 2:
Create a text file hello.py
print ("Hello")
Execute the command
python hello.py
Output
--------
Hello
Data Types
No need to define data types (Dynamically Typed Language ). Directly variable mapping can be done
example:
name = "Freeza"
number = 5
where name is treated as string and number is treated as integer. Its developer friendly but when it goes big it has some its own draw back as the IDE itself will not handle the issues for which Python 3 has some solution.
Type hinting
example:
def print_it (a: int ) -> int:
return a
if __name__ == '__main__':
value = print_it(5)
print(value)
Integers
- without dot are integers
- with dot are floats
sample = 45
newvalue = 55.53
int(newvalue) # 55
float(sample) # 45.0
Strings
- word
- paragraphs
- characters
* Ascii text format in python 2 and Unicode format in python 3
example
single quotes -- 'sample 1'
double quotes -- "sample 2"
triple double quotes -- """sample 3 """ # used for documentation
number of string functions are available for string definition.
example
- capitalise
- check if its alphabet or not
- check if its a digit
- replace characters
- csv format to value array/list (split)
Printing it on the go
text1 = "super"
text2 = "Dragonballs"
"They have something {0} called {1} in their planet".format(text1,text2)
f"They have something {text1} called {text2} in their planet"
f - format
r - raw string - doesn't do any preprocessing
u - unicode string
Slicing
s = "Dear Freeza i give you a last chance provide an apology and go away"
s[10:20]
'a i give y'
Boolean
- True
- False
example:
isGokuAlive = True #int(isGokuAlive) #str(isGokuAlive)
isGohanEating = False #int(isGohanEating)
None
- equally as null
Control Statements
If Statement
def is_equal(number):
if number == 7: # == is for checking equality isequal
print("there are 7 dragon balls")
else:
print("there are not 7 dragon balls")
number assigned other than zero has true value.
use "and" and "or" for multiple condition
List
characters = ["goku","freeza","cell"]
characters[0]
>> 'goku'
characters[-1]
>>'cell'
characters.append("gohan")
characters
>>['goku', 'freeza', 'cell', 'gohan']
"goku" in characters
>>True
len(charecters)
>>4
del charecters[2]
charecters
>>['goku', 'freeza', 'gohan']
List Slicing
charecters[1:]
['freeza', 'gohan']
charecters[1:-1]
>>['freeza']
Operations
- push
- pop
- reverse
list comprehension
printing elements in list using loop for printing every elements
for chars in characters:
print("{0}".format(chars))
>>goku
>>freeza
>>gohan
a =0
for count in range(7):
a+=1
print("the number of dragon balls collected is {0}".format(a))
the number of dragon balls collected is 1
the number of dragon balls collected is 2
the number of dragon balls collected is 3
the number of dragon balls collected is 4
the number of dragon balls collected is 5
the number of dragon balls collected is 6
the number of dragon balls collected is 7
range(7)
range(1,10)
range(1,10,2)
Break and Continue
def check_break(pass_list):
for available in pass_list:
if available == "gohan":
print( available +" is available for a fight")
print(available + "is currently checked for availability")
if __name__ == '__main__':
dbz_chars=["goku","pikalo","gohan","kralin","Bulma","chichi","dende","vegeta"]
check_break(dbz_chars)
goku is currently checked for availability
pikalo is currently checked for availability
gohan is available for a fight
gohan is currently checked for availability
kralin is currently checked for availability
Bulma is currently checked for availability
chichi is currently checked for availability
dende is currently checked for availability
vegeta is currently checked for availability
def check_break(pass_list):
for available in pass_list:
if available == "gohan":
print( available +" is available for a fight")
break
print(available + " is currently checked for availability")
if __name__ == '__main__':
dbz_chars=["goku","pikalo","gohan","kralin","Bulma","chichi","dende","vegeta"]
check_break(dbz_chars)
goku is currently checked for availability
pikalo is currently checked for availability
gohan is available for a fight
def check_break(pass_list):
for available in pass_list:
if available == "Bulma":
continue
print( available +" is available for a fight")
print(available + " is currently checked for availability")
if __name__ == '__main__':
dbz_chars=["goku","pikalo","gohan","kralin","Bulma","chichi","dende","vegeta"]
check_break(dbz_chars)
goku is currently checked for availability
pikalo is currently checked for availability
gohan is currently checked for availability
kralin is currently checked for availability
chichi is currently checked for availability
dende is currently checked for availability
vegeta is currently checked for availability
def while_loop(x):
while x < 7:
print("The number is {0}".format(x))
x+=1
if __name__ == '__main__':
while_loop(0)
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
Dictionaries
- store key value pairs
chars = {
"name": "Goku",
"role": "Hero",
"fans": "infinity"
}
chars.values()
>> dict_values(['Goku', 'infinity', 'Hero'])
chars.keys()
>>dict_keys(['name', 'fans', 'role'])
chars.get("name")
>>'Goku'
chars["name"] == "goten"
>>False
chars["name"] = "goten"
chars
>>{'name': 'goten', 'fans': 'infinity', 'role': 'Hero'}
del chars["role"]
chars
>>{'name': 'goten', 'fans': 'infinity'}
Exception
chars = {
"name": "Goku",
"role": "Hero",
"fans": "infinity"
}
try:
power_level = chars["power_level"]
except KeyError: #except Exception: #except TypeError as error -> print(error)
print("handled exception")
Other Data Types
- long
- byte
-bytearray
- tuples , similar to list - immutable values
- set and frozenset - > no duplicates
list to set conversion will remove the duplicates as well as arranges it in ascending order
Comments
Post a Comment