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

Python Commands

Python to be done with the game time-wasters

Uploaded by

siswand.1989
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
225 views

Python Commands

Python to be done with the game time-wasters

Uploaded by

siswand.1989
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Basic Commands

1. print() - Displays output.


Syntax: print("Hello, World!")

2. input() - Takes user input as a string.


Syntax: name = input("Enter your name: ")

3. len() - Returns the length of a sequence (string, list, etc.).


Syntax: length = len("Hello")

4. type() - Returns the data type of an object.


Syntax: data_type = type(123)

Data Type Conversions


5. int() - Converts a value to an integer.
Syntax: num = int("123")

6. float() - Converts a value to a float.


Syntax: flt = float("12.3")

7. str() - Converts a value to a string.


Syntax: txt = str(123)

8. list() - Converts a value to a list.


Syntax: lst = list("abc") # ['a', 'b', 'c']

Control Flow Statements


9. if, elif, else - Conditional statements.
Syntax:
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")

10. for - For loop iteration.


Syntax:
for i in range(5):
print(i)

11. while - While loop iteration.


Syntax:
while x < 5:
print(x)
x += 1

12. break - Stops the loop.


Syntax:
for i in range(10):
if i == 5:
break
print(i)

13. continue - Skips the current iteration.


Syntax:
for i in range(5):
if i == 2:
continue
print(i)

14. pass - Does nothing (used as a placeholder).


Syntax:
if True:
pass # Placeholder for future code

File Management
15. open() - Opens a file in a specified mode.
Syntax: file = open("filename.txt", "r")

16. read() - Reads the file's content.


Syntax: content = file.read()

17. write() - Writes data to a file.


Syntax: file.write("Hello, World!")

18. close() - Closes a file.


Syntax: file.close()

Functions and Modules


19. def - Defines a function.
Syntax:
def greet(name):
print(f"Hello, {name}!")

20. return - Returns a value from a function.


Syntax:
def add(a, b):
return a + b

21. import - Imports a module.


Syntax: import math

22. from ... import ... - Imports specific items from a module.
Syntax: from math import sqrt

23. as - Assigns an alias to a module.


Syntax: import numpy as np

Exception Handling
24. try / except / finally - Catches exceptions.
Syntax:
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")
finally:
print("End of code.")

Classes and Objects


25. class - Defines a class.
Syntax:
class Person:
def __init__(self, name):
self.name = name

26. self - Refers to the instance of a class.


Syntax:
def greet(self):
print(f"Hello, {self.name}")

Miscellaneous Commands
27. help() - Shows help for a function or module.
Syntax: help(len)

28. dir() - Lists attributes and methods of an object.


Syntax: dir(str)

29. eval() - Evaluates a string as Python code.


Syntax: result = eval("3 + 5")

30. exec() - Executes dynamic Python code.


Syntax: exec("x = 5; print(x)")

You might also like