Python Tutorial
#!/usr/bin/python print "First Python Code!";here operating system will use first line to know which interpreter to run to execute the scripts if python scripts file is made as executable file.
$python test.pyFor this case, first line #!/usr/bin/python is not required.
$ chmod +x test.py # Makes the python file executable $./test.pyOutput:
First Python Code!
#!/usr/bin/python input_str = raw_input("Please enter a string: ") print("Entered string is %s" % input_str)Output:
$python first_input.py Please enter a string: welcome to python Entered string is welcome to pythonSuppose we need to get input as integer format, type casting needs to be done as below.
# file name is first_input.py #!/usr/bin/python num = int(raw_input("Please enter an integer: ")) print("Entered number is %d" % num)Output:
$python first_input.py Please enter an integer: 34 Entered number is 34Python interpreter throws type error if print statement incorrectly uses type.
$ python first_input.py Please enter a string: welcome to python Traceback (most recent call last): File "first.py", line 5, in« Previous Next »print("Entered string is %d" % input_str) TypeError: %d format: a number is required, not str
Python Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page