DEV Community

imrinzzzz
imrinzzzz

Posted on • Updated on

Basic Python Programming (part 1)

Writing Python in Command Prompt

Start: Python (needs to install Python PATH first) > cmd shell (Window + R > cmd) > type 'python'

End: Ctrl + z, or exit()

printing

::syntax::

>>> print("string")
>>> print(variable)
>>> print(
... "string"
... )
>>> print("Hello world");\
... print("next line eiei")

string

>>> print("this is" + " appending")
>>> print(len("len = string length"))

variable

>>> variable_name = "any type value, but this is string"
>>> example = 10;\
... print(example)  //output: 10

variable types: bool (true, false), int (integer), float (rational number), str (string)

::conversion::
int(x)
float(x)
str(x)

::syntax::

>>> year = 1999; month = 'March'
>>> print(f"Birthday is {month}, {year}") //output: Birthday is March, 1999
>>> x = 5; y = 10
>>> print(x, y) //5 10

FILE

open / write / close
>>> variable_file_name = open("destination/file_name.txt", "w") //w is open new file (reset the file)
>>> variable_file_name.write("string \n means new line")
>>> variable_file_name.close()
read
>>> file = open("destination/file_name.txt", "r") //r is read only which means cannot edit
>>> file.read() //read all
>>> file.read(n) //read n characters
>>> file.readline //read a line
>>> file.close()
append
>>> file = open("destination/file_name.txt", "a") //a means appending the file
>>> file.write("add/append text here")
>>> file.close()

Top comments (0)