Python: Printing, Commenting, variables and Moodle Tasks
- Printing -
In python the "print" function prints a specific piece message to a screen or another standard output device. The message could be used as a string or another object, where the object would be converted into a string before it is presented onto the screen.
In a regular python editor not involved with a 3D Animation/editing software to print a message/piece of test you would write the following:
Input: print("May The Force Be With You")
output: May The Force Be With You
To print more than one object you would write the following:
Input: print("One Shall Stand", "And One Shall Fall")
Output: One Shall Stand One Shall Fall
To print a Tuple you would write the following:
Input: x = ("Autobot", "Decepticon", "Human")
print(x)
Output : ("Autobot", "Decepticon", "Human")
To print two messages and specify the separator you would write the following:
Input: print("To be or not to be", "is that the question?", sep="---")
Output: To be or not to be---Is that the question?
When it comes to printing something in the Maya script editor you would add a little bit of extra text before the print command so that the script editor recognises the command. To generate a Poly-Cube within Maya you would write the following within the script editor:
Input: import maya.cmds as cmds
cmds.polyCube()
Output: 1 Poly-Cube
You would use this exact same command to generate of 3D objects but instead of writing "PolyCube" you would put "PolySphere" and that would generate a 3D Sphere. Now the one thing that makes this piece of code work is "import maya.cmds as cmds" , this allows Maya to recognise the command within the Python Editor so that a piece of code would work as you intend it to. As such when using the Python editor with in Maya always use this piece of text before writing out the rest of your code so that it works and produces what you intend to.
- Commenting -
Within Python Comments can be used to explain pieces of Python code, to make code more readable and easier to understand, but also comments can be used to prevent the activation of a piece of code when you are testing it. To make a comment within Python you would write out the following:
Input: #This is a comment
print("Hello There!")
Output: Hello There!
To produce a Comment within python you would place a hashtag "#" in front of whatever piece of code that you would be using. With Comments you can also place them after the piece of code that you are going to execute within python, for example:
Input: print("Hello, Earth!") #This is a comment
Output: Hello, Earth!
This produces the exact same outcome as the previous examples that I had stated earlier of how comments work within python.
To prevent a piece of code from being executed within python you would write out the piece of code like the following:
Input: #print("Hello, There!")
print("General Kenobi!")
Output: General Kenobi
With this piece of code by placing a hashtag "#" in front of the print command, it prevented it from activating so only the second command that you had written executed, and if you wanted multi line comments of code to be hidden or prevented from being executed, you would just place a hashtag "#" in front of each line like the following:
Input: #This is my comment
#written in
#more than just one line
#by just using a hashtag
print("Hi, Bob!")
Output: Hi Bob
This becomes very helpful when you have large amounts of code for a piece of 3D Animation or generic coding and you only need to test a fragment of it to see if each piece works.
- Variables -
In Python Variables are typically used as containers for storing data values. Within Python there is no set command for declaring a variable, but one is created as soon as the moment you assign a value to it like the following:
Input: x = 5
y = "Apples"
print(x)
print(y)
Output: 5
Apples
Variables don't need to be declared with any specific type, and it can even change after they have originally been set. For example:
Input: x = 4 # x is of type int
x = "bob" # x is now of type str
print(x)
Output: bob
When it comes to python in Maya you can use variables in near enough the exact same way, where if you try to create a specific object with different values you can use these methods by assigning specific entity's to create one.
-Moodle tasks-
Task 1:
- Try to run the following two scripts:
Input 1. print("Hi","there,","how are you?")
print("Hi", "there," , "how are you?")
and
Input 2. print("Hi","there,","how are you?")
print("Hi","there, ","how are you?")
Output 1: Hi there, how are you?
Hi there, how are you?
Output 2: Hi there, how are you?
Hi there, how are you?
Question = Why do the first two lines produce the same output, while the last two result in different outputs?
Answer = When you want to print a message in python, within the brackets you would use quotation marks at the start and end of what you want to say. Now for the reason why Script one has a different output than script 2 is because these quotation marks are used to judge the spacing between the message that you are trying to print, so the closer it is to the message you are printing the less space you get, but like script 2 the further away you leave the quotation mark the bigger amount of space you get between each piece of the message you are trying to print.
Task 2:
print("I will now count my chickens:")
print("Hens", 25 + 30 / 6)
print("Roosters", 100 - 25 * 3 % 4)
print("Now I will count the eggs:")
print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)
print("Is it true that 3 + 2 < 5 - 7?")
print(3 + 2 < 5 - 7)
print("What is 3 + 2?", 3 + 2)
print("What is 5 - 7?", 5 - 7)
print("Oh, that's why it's False.")
print("How about some more.")
print("Is it greater?", 5 > - 2)
print("Is it greater or equal?", 5 >= - 2)
print("Is it less or equal?", 5 <= - 2)
From running this script, I have concluded that the following Numerical symbols mean the following:
1. + is used to add values together in an equation.
2. - is used to subtract values within the equation.
3. < is used in a piece of code to conclude if a certain value is less than another value.
4. > is used in a piece of code to conclude if a certain value is greater than another value.
5. <= is used to conclude if a value is less than or equal to another value within a piece of code.
6.>= is used to conclude if a value is greater than or equal to another value within a piece of code.
7. * is used to multiply values on either side of the operator.
8. / is used for in Python to divide the left hand operand by the right hand operand.
9. % is used for in python to divide the left hand operand by the right hand operand and returns the remainder.
Task 3 - Christmas Tree code
Input:
def draw_tree(height):
for i in range(1, height + 1):
for j in range(height - i):
print(" ", end="")
for j in range(2 * i - 1):
print("*", end="")
print()
draw_tree(5)
Output:
* *** ***** ******* *********
Programming for Computer Animation and VFX TB1
Status | In development |
Category | Other |
Author | Aronne_Monk |
More posts
- Python: Type() and FactorialsDec 11, 2022
- Python: Functions, Arguments and ReturnDec 11, 2022
- Python: Integers, Floating Point Numbers, Complex Numbers, Collections/lists and...Dec 11, 2022
Leave a comment
Log in with itch.io to leave a comment.