DEV Community

imrinzzzz
imrinzzzz

Posted on • Edited on

6 1

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()

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay