Python: Difference between revisions
From Cheatsheet
Jump to navigationJump to search
No edit summary |
No edit summary |
||
| Line 4: | Line 4: | ||
# Print "Hello World" | # Print "Hello World" | ||
print("Hello | >>> print("Hello World!") | ||
Hello World! | |||
# Add values together | |||
>>> 2 + 3 | |||
5 | |||
# Find out the type of a value | |||
>>> type (1 + 2) | |||
<class 'int'> | |||
>>> type (4 / 2) | |||
<class 'float'> | |||
# Two to the power of 3 | |||
>>> 2 ** 3 | |||
8 | |||
# Echo 3 strings | |||
>>> "Hello" + " " + "World" | |||
'Hello World' | |||
# Define a variable | |||
>>> a = 3 | |||
# Display the contents of a variable | |||
>>> a | |||
3 | |||
# Add a newline to a value | |||
>>> print("Hello World!\n") | |||
Hello World! | |||
# Escape an escape character | |||
>>> print("Hello World\\n") | |||
Hello World\n | |||
# Single bracket are usable inside double-quotes | |||
>>> "Hello 'orld" | |||
"Hello 'orld" | |||
# Escape a double bracket | |||
>>> "Hello \"World" | |||
'Hello "World' | |||
# Display the lowest or highest value of a set values | |||
>>> min(5, 1, 8) | |||
1 | |||
>>> max(5, 1, 8) | |||
8 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 11:27, 7 February 2024
Basic Syntax
# Leave a comment by prepending a "#" to your line
# Print "Hello World"
>>> print("Hello World!")
Hello World!
# Add values together
>>> 2 + 3
5
# Find out the type of a value
>>> type (1 + 2)
<class 'int'>
>>> type (4 / 2)
<class 'float'>
# Two to the power of 3
>>> 2 ** 3
8
# Echo 3 strings
>>> "Hello" + " " + "World"
'Hello World'
# Define a variable
>>> a = 3
# Display the contents of a variable
>>> a
3
# Add a newline to a value
>>> print("Hello World!\n")
Hello World!
# Escape an escape character
>>> print("Hello World\\n")
Hello World\n
# Single bracket are usable inside double-quotes
>>> "Hello 'orld"
"Hello 'orld"
# Escape a double bracket
>>> "Hello \"World"
'Hello "World'
# Display the lowest or highest value of a set values
>>> min(5, 1, 8)
1
>>> max(5, 1, 8)
8
Template