Let's begin with a simple program:
Create a text file and save it as firstProgram.py, put in the following code
print("Hello World!")
save the file and then open the command line and navigate to the folder where the firstProgram.py file is and execute the command
>>>python3 firstProgram.py
Hello World!
Our first program is complete, you may note that the syntax for a function is function name with braces, similar to Java. print() is a built-in function and built-in functions don't need any import statements, they are part of the core python.
Edit the file again and let's add some more code to showcase how strings act.
# First Program
print("Hello World!")
print('We\'re coming home!')
print('Hi '+'there!')
print('Hi', 'buddy')
print('Hi', 5)
Execute the above statements in the file and the output will be
Hello World!
We're coming home
Hi there!
Hi buddy
Hi 5
Now from the above lines we can observe that
- Anything followed by a # will be ignored and # acts as a single line comment, multiline comments: ''' ending with '''
- Strings can be enclosed in both single and double quotes, just like javascript and the escape character is backslash "\"
- String concatenation works pretty much as expected with a + sign in-between,
- however the print function accepts many strings and other data types which can be appended if given comma separated but the print function not only appends the string but also adds a space between them.
- The print function also adds a newline after the string is printed.
Whole list of inbuilt functions like print() are listed in the documentation here: https://docs.python.org/3.5/library/functions.html but we will rarely use most of them.
No comments:
Post a Comment