Jul 15, 2024
>>>
, code runs individually"hello" + "world"
"hello" * 3
ā hellohellohello
spam = "hello"
# single line
and ''' multi-line '''
print("hello", spam)
input("Enter your name: ")
",".format(variable)
len("hello")
ā 5str(5)
, int(5.0)
==
, !=
is
, is not
, and
, or
if name == "Alice": print("Hi Alice")
if-else
:
if name == "Alice":
print("Hi Alice")
else:
print("Hello stranger")
elif
(else-if):
if name == "Alice":
print("Hi Alice")
elif name == "Bob":
print("Hi Bob")
else:
print("Hello stranger")
while spam < 5:
print(spam)
spam += 1
while True:
if condition:
break
for i in range(5): print(i)
range(start, stop, step)
import random
from random import *
def hello(name):
print("Hello", name)
def get_answer(number):
return "Yes"
spam = ["cat", "bat", "rat"]
spam[0]
ā cat
, spam[-1]
ā rat
spam[0:2]
ā ['cat', 'bat']
spam_copy = spam[:]
spam.append("dog")
spam.sort()
spam = {"size": "fat", "color": "gray"}
spam["size"]
ā fat
.keys()
, .values()
, .items()
, get()
, setdefault()
t = (1, 2, 3)
list(t)
, tuple(spam)
set()
, {1, 2, 3}
.add()
, .update()
, .remove()
, .discard()
, .union()
, .intersection()
, .difference()
, .symmetric_difference()
[i-1 for i in a]
{s.upper() for s in b}
{k: v for k, v in d.items()}
\n
, \'
, \t
r"\n"
'''text'''
spam[0:5]
, spam[:5]
, spam[5:]
.upper()
, .lower()
, .startswith()
, .endswith()
, .join()
, .split()
, .strip()
try:
1/0
except ZeroDivisionError:
print("Error")
try:
pass
finally:
print("Always executed")
lambda x, y: x + y
(lambda x, y: x + y)(5, 3)
print('kid') if age < 18 else print('adult')
*args
and **kwargs
: Allows variable number of argumentsraise Exception("Error message")
import logging
logging.basicConfig()
logging.debug('message')
if __name__ == "__main__":
pass
Note: Explore more on advanced topics like classes and objects for a complete understanding of Python.
š Happy Coding! š