In this blog you’ll learn about the different kinds of data you can work with in
your Python programs. You’ll also learn how
to store your data in variables and how to use
those variables in your programs. Open your text editor (VS Code) or any you are comfortable with.
Similarly, the """triple quotes""" indicates multi-line comment. For example:
No matter whichever programming language you learn, the first program you will write is Hello World program. So, let's write your first python program. All the codes in this blog are italic.
Create a new file and save it as "hello_world.py". '.py' extension will tell VS Code it is a python code and run a python interpreter to run it. So, make sure to add '.py' at the end of filename.
hello_world.py
print("Hello Python world!")
To run this code, the keyboard shortcut is 'F5' ,if you get prompted then click enter.
You successfully run your hello_world program. Now, let's learn about variables.
Variables
Every variable holds a value, which
is the information associated with that variable. In python, you don't need to declare or initialize variables unlike any other programming languages.
message = "Hello Python world!"
print(message)
Here, "message" is our variable and it holds a value of "Hello Python world!" and while printing the value hold by message get's printed.
Rules for naming and using variables
Variable names can contain only letters, numbers, and underscores.
They can start with a letter or an underscore, but not with a number.
For instance, you can call a variable message_1 but not 1_message.
- Spaces are not allowed in variable names, but underscores can be used to separate words in variable names. For example, greeting_message works, but greeting message will cause errors.
- Avoid using Python keywords and function names as variable names; that is, do not use words that Python has reserved for a particular programmatic purpose, such as the word print.
- Variable names should be short but descriptive. For example, name is better than n, student_name is better than s_n, and name_length is better than length_of_persons_name.
Strings
Strings are quite simple at first glance,
but you can use them in many different ways.
A string is simply a series of characters. Anything inside quotes is considered a string in Python, and you can use single or double quotes around
your strings like this:
"This is a string."
'This is also a string.'
String Operations
Changing Case
name = "prasanna dhungel"
- name.title() converts name to camel case.i.e. Prasanna Dhungel.
- name.upper() converts name to upper case. i.e. PRASANNA DHUNGEL
- name.lower() converts name to lower case. i.e. prasanna dhungel
Combining or Concatenating Strings
first_name = "Prasanna"
last_name = "Dhungel"
full_name = first_name + " " + last_name
print(full_name) gives us "Prasanna Dhungel".
Let's combine and use what we have learned till now.
message = "Hello, " + full_name.title() + "!"
print(message)
Adding Whitespace to Strings with Tabs or Newlines
\t indicates tabs
\n indicates new line
print("Languages:\nPython\tJava\tGo")
Numbers
Numbers are used quite often in programming to keep score in games, represent data in visualizations, store information in web applications, and so
on. Python treats numbers in several different ways, depending on how they
are being used. Integers are numbers without decimal point. Python calls any number with a decimal point a float.
Some operations on numbers are:
add (+), subtract (-), multiply (*), divide (/) and exponents(**)
print(10+6) gives 16.
print(10-6) gives 4.
print(10*6) gives 60.
print(10/5) gives 2.
print(3**3) gives 27.
In most cases, you will want to print integer concatenating with strings. This can't be done directly so we convert integers to strings in such case. For example,
age = 23
message = "Happy " + age + "rd Birthday!"
# This line raises error "TypeError: Can't convert 'int' object to str implicitly "
So, we need to convert age to string before. str() function is returns string datatypes.
message = "Happy " + str(age) + "rd Birthday!"
Comments
Comments are an extremely useful feature in most programming languages.
Everything you’ve written in your programs so far is Python code. As your
programs become longer and more complicated, you should add notes within
your programs that describe your overall approach to the problem you’re
solving. A comment allows you to write notes in English within your programs.
In Python, the hash mark (#) indicates a single line comment. Anything following a
hash mark in your code is ignored by the Python interpreter. For example:
# Say hello to everyone.
"""
This
is a
multi-line comment.
"""
Conclusion
In this blog you learned to work with variables. You learned to use
meaningful variable names and how to avoid name errors. You learned what strings are and how to
display strings using lowercase, uppercase, and camelcase. You started
using whitespace to organize output neatly. You started working
with integers and floats, and you read about some unexpected behavior
to watch out for when working with numerical data. Finally, you also learned to
write explanatory comments to make your code easier for you and others
to read.
Post a Comment
Let's make it better!
Comment your thoughts...